为什么 cout << setprecision(2) << 0.999 的输出是 1 而不是 1.0?

Why output of cout << setprecision(2) << 0.999 is 1 instead of 1.0?

本文关键字:lt 输出 setprecision cout 为什么      更新时间:2023-10-16

显着数字为2。

为什么输出

cout << setprecision(2) << 0.999 << endl;` 

1而不是1.0

默认格式不打印尾随零;您需要将浮点格式设置为fixed,另请参阅此参考。所以你需要的是

cout << setprecision(2) << fixed << 0.999 << endl;

还请注意,SetPrecision是指十进制数字,因此对于1.0,您需要setprecision(1)