我有一个字符串变量,想在没有任何科学符号的情况下把它转换成双数。我尝试使用 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)为双数?
科学符号与 std::cout
,而不是存储值的方式,所以你必须使用 std::fixed
在你打印数值之前。
std::cout << std::fixed << std::setprecision(2) << d << std::endl;
正如你在演示中所看到的,这很好用,你也应该可以用。
如 @goodvibration评论道 std::to_string
也可以使用,但不能简单地重新定义默认的数字或小数点。
std::cout << std::to_string(d) << std::endl;