Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有效数字为 2。
为什么输出
cout << setprecision(2) << 0.999 << endl;`
是1而不是1.0?
1
1.0
默认格式不打印尾随零;您需要将浮点格式设置为fixed,另请参阅此参考。所以你需要的是
fixed
cout << setprecision(2) << fixed << 0.999 << endl;
还要注意 setprecision 是指十进制数字,所以对于 1.0 你需要setprecision(1)
setprecision(1)