如何使用 cout 以全精度打印双精度值? [英] How do I print a double value with full precision using cout?
本文介绍了如何使用 cout 以全精度打印双精度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的之前的问题中,我打印了一个double
使用 cout
在我没想到的时候被四舍五入了.如何使 cout
使用全精度打印 double
?
In my earlier question I was printing a double
using cout
that got rounded when I wasn't expecting it. How can I make cout
print a double
using full precision?
推荐答案
您可以直接在 std::cout
上设置精度并使用 std::fixed
格式说明符.
You can set the precision directly on std::cout
and use the std::fixed
format specifier.
double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;
您可以#include
获得浮点数或双精度数的最大精度.
You can #include <limits>
to get the maximum precision of a float or double.
#include <limits>
typedef std::numeric_limits< double > dbl;
double d = 3.14159265358979;
cout.precision(dbl::max_digits10);
cout << "Pi: " << d << endl;
这篇关于如何使用 cout 以全精度打印双精度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文