将货币格式的字符串转换为 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/