c++ - 如何计算具有 n 位小数的 float

这个问题在这里已经有了答案:
关闭9年前

Possible Duplicate:
How do I print a double value with full precision using cout?

float a = 175.;
   cout << a;

如果我运行之前的代码,我只会得到 175,我怎么能用(例如)3 个小数位计算出这个数字,即使它们是零。我怎样才能打印“175.000”?!

最佳答案

您需要 std::fixedstd::setprecision :

 std::cout << std::fixed << std::setprecision(3) << a;

这些需要以下标题:

#include <iomanip>

https://stackoverflow.com/questions/14677448/

相关文章:

html - float 子元素 : overflow:hidden or clear:both?

bash - 在bash中右对齐/填充数字

java - 如何在本地时区将 java.time.Instant 格式化为字符串?

unicode - 如何破解 GHCi(或 Hugs)以便打印未转义的 Unicode 字符?

formatting - htop 输出到人类可读文件

android - 在 android textview 中显示