为什么 cout << setprecision(2) << 0.999 的输出是 1 而不是 1.0?
Why output of cout << setprecision(2) << 0.999 is 1 instead of 1.0?
显着数字为2。
为什么输出
cout << setprecision(2) << 0.999 << endl;`
是1
而不是1.0
?
默认格式不打印尾随零;您需要将浮点格式设置为fixed
,另请参阅此参考。所以你需要的是
cout << setprecision(2) << fixed << 0.999 << endl;
还请注意,SetPrecision是指十进制数字,因此对于1.0,您需要setprecision(1)
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 呼叫运营商<<临时
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 过载输出<<用于类的运算符,以打印其中的元组
- 重载运算符<<输出地址而不是数据成员
- 什么会导致死亡<<变量& lt; & lt;“ t";当输出超过终端宽度时,打印空白而不是值
- Can运算符<<用于设计我们自己的输出显示方式