如何计算具有 n 位小数的浮点数
How to cout a float number with n decimal places
可能的重复项:
如何使用 cout 以全精度打印双精度值?
float a = 175.;
cout << a;
如果我运行以前的代码,我将只得到 175,我怎么能用(例如)3 位小数的数字来计算数字,即使它们是零......如何打印"175.000"?!
你需要std::fixed
和std::setprecision
:
std::cout << std::fixed << std::setprecision(3) << a;
这些需要以下标头:
#include <iomanip>
尝试setprecision
:
cout.setf(ios::fixed);
cout << setprecision(3) << a << endl;
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 使用提升将数据从 PyObject 复制到浮点数 *
- 使用浮点数和双精度数的非常小数字的数学
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 返回浮点数的小数位数
- txt 文件中浮点数的最大和最小值
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 在数学上将浮点数四舍五入到 N 位小数
- 从浮点数中删除小数部分但保留类型的有效方法
- 小数浮点数缺少除法 (C++)
- 如何计算C++浮点数中的小数位数
- 将浮点数四舍五入为2位小数,将其写入文本文件
- 使用 6 位设置浮点数的小数部分
- 将浮点数截断为前导N位小数
- 精确地提取浮点数的小数部分
- 将浮点数打印为小数并修复错误输出
- 如何计算具有 n 位小数的浮点数