我有一个 string_to_number 函数,可以将字符串转换为 double 。为什么这在这种情况下不起作用?
#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;
}
最佳答案
这个数字太大,无法放入一个 double 中,因此它被截断了。
关于c++ - 这个 string_to_number 函数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21601489/