本文介绍了舍入为小数点后2位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的命令将值舍入为小数点后两位:
I've used the following to round my values to 2 decimal points:
x = floor(num*100+0.5)/100;除了16.60这个值为16.6的值。
and this seems to work fine; except for values like "16.60", which is "16.6".
我想输出这个值,例如16.60。
I want to output this value like "16.60".
我输出值的方式如下:
cout setw(12) << round(payment);我试过下面的:
cout setw(12) << setprecision(2) << round(payment);但这给了我很多的答案。
But that gave me answers like
1.2e+02如何正确输出值?
推荐答案这是因为 std :: setprecision 小数点后的数字,但如果不改变浮点格式以使用小数点后的固定数字,则为有效数字。要更改格式,您必须将 std :: fixed 输入到输出流中:
This is because std::setprecision doesn't set the digits after the decimal point but the significant digits if you don't change the floating point format to use a fixed number of digits after the decimal point. To change the format, you have to put std::fixed into your output stream:
double a = 16.6; std::cout << std::fixed << std::setprecision(2) << a << std::endl;更多推荐
舍入为小数点后2位
发布评论