【发布时间】:2020-06-11 23:09:16
【问题描述】:
我有一个字符串变量,想在没有任何科学记数法的情况下将其转换为双精度。我尝试使用std::stod,但这不起作用。
std::stringstream timestamp;
timestamp << t_val;
string test = timestamp.str();
cout << test << endl; // this gives 1506836639.96
double d = std::stod(test);
cout << d << endl; // This gives 1.50684e+09 instead of 1506836639.96
我尝试使用setprecision 和fixed,但我无法将结果存储到变量中。有没有办法可以将test (1506836639.96) 的值存储为双精度值?
【问题讨论】:
-
科学记数法在打印输出中,而不是在
double值中。 -
使用这个值进行计算,但是当你最终想要打印它时,通过
std::to_string将它转换回字符串。
标签: c++ string double precision scientific-notation