在c ++中显示小数点后的两位数字 [英] Show two digits after decimal point in c++
本文介绍了在c ++中显示小数点后的两位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
论坛中已讨论过类似主题。但我在下面的代码有一些不同的问题:
Similar topic is already discussed in the forum. But I have some different problem in following code:
double total;
cin>>total
cout<<fixed<<setprecision(2)<<total;
如果我输入为100.00,那么程序打印的只是100而不是100.00
If I give input as 100.00 then program prints just 100 but not 100.00
如何打印100.00?
How can I print 100.00?
推荐答案
cout << fixed << setprecision(2) << total;
setprecision
/ em>精度。
setprecision
specifies the minimum precision. So
cout << setprecision (2) << 1.2;
将打印1.2
fixed
表示小数点后面将有固定数目的小数位
fixed
says that there will be a fixed number of decimal digits after the decimal point
cout << setprecision (2) << fixed << 1.2;
将打印1.20
这篇关于在c ++中显示小数点后的两位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文