c++ - 将货币格式字符串转换为 double

标签 c++ formatting

将货币格式的字符串转换为 double 的最简单方法是什么

例如,1,234,567.00 到 1234567.00

string replace 结合 stringstream 是我目前最好的选择。

我还没有使用 C++11。所以,http://en.cppreference.com/w/cpp/io/manip/get_money 不是一个选项

最佳答案

您可以使用 C 函数 atof,在删除所有“,”字符后:

#include <string>
#include <algorithm>
#include <stdlib.h>

double strToDouble(string str)
{
    str.erase(remove(str.begin(), str.end(), ','), str.end());
    return atof(str.c_str());
}

它也可以在不使用任何 C++11 特性的情况下工作。

关于c++ - 将货币格式字符串转换为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875678/

相关文章:

python - 格式化字符串的最 Pythonic 方式

c# - 如果数字为零,则显示自定义字符的格式函数

c++ - 了解模板偏特化

html - 如何更改eclipse html格式

java - Android native 库从 aar 链接到另一个 native 库

c++ - 'led' 之前的预期主表达式

c - 如何使具有多个输入的 scanf() 忽略其他输入?

c++ - 将 double 指数的可移植打印到 C++ iostreams

C++ std::stringstream 到 const char* 的转换

c++ - 如何在 qt 中执行异步文件 io?