如何在小数点后保留0,将字符串转换为双精度

How to retain 0s after decimal point, after converting string to double

本文关键字:字符串 转换 双精度 小数点 保留      更新时间:2023-10-16
#include <iostream>
using namespace std;
int main()
{
    string str="-1.2300";
    double d = stod(str);
    cout<<d<<"n";
}

输出:-1.23

我期望输出如下

-1.2300

如果这是您想要的,那么您需要使用带精度说明符的格式化输出:

printf("%.4fn", d);

或指定cout的精度:

cout.precision(4);
cout << fixed << d << "n";