如何处理带有n个小数位的浮点数 [英] How to cout a float number with n decimal places
本文介绍了如何处理带有n个小数位的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
float a = 175.;
cout << a;
如果我运行上面的代码,我会得到只有175,例如)3个小数位,即使它们是零。我如何打印175.000?!
If I run the previous code I'll get just 175, how can I cout the number with (for example) 3 decimal places even they were zeros .. How can I print "175.000" ?!
推荐答案
href =http://en.cppreference.com/w/cpp/io/manip/fixed> std :: fixed
和 std :: setprecision
:
You need std::fixed
and std::setprecision
:
std::cout << std::fixed << std::setprecision(3) << a;
这篇关于如何处理带有n个小数位的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文