我有一个string_to_number函数可以将字符串转换为double。为什么在这种情况下这不起作用?这个string_to_number函数有什么问题?
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
double string_to_number(string text)
{
double value;
istringstream (text) >> value;
return value;
}
int main()
{
string text = "1234567890987654321";
double value = string_to_number(text);
cout << fixed << setprecision(0) << value << endl; // 123456789098765400 ??? What happened to "321" ?!!
return 0;
}
没有什么是错的功能。 [阅读本文](http://www.validlab.com/goldberg/paper.pdf)。 – 2014-02-06 11:24:45
你*有*写自己的?你不能使用例如['std :: stoll'](http://en.cppreference.com/w/cpp/string/basic_string/stol)(或['std :: stold'](http://en.cppreference.com/) W/CPP /串/ basic_string的/ STOF))。 –
您是否尝试以同样的方式执行std :: cout << 1234567890987654321.'? – Jarod42