2
的显著数字是2为什么输出cout << setprecision(2)<< 0.999是1而不是1.0?
为什么的
cout << setprecision(2) << 0.999 << endl;`
输出为1
,而不是1.0
?
的显著数字是2为什么输出cout << setprecision(2)<< 0.999是1而不是1.0?
为什么的
cout << setprecision(2) << 0.999 << endl;`
输出为1
,而不是1.0
?
默认格式不打印尾随零;您需要将浮点格式设置为fixed
,另请参阅this reference。所以,你需要的是
cout << setprecision(2) << fixed << 0.999 << endl;
还要注意setprecision指十进制数字,所以对于1.0则需要setprecision(1)
你的意思是'endl'。对? –
@TalhaIrfan是的,我的意思是endl –
我希望下面的答案解决您的问题。请接受他的答案,如果它可以关闭它! –