如何使用cout以全精度打印双精度值? [英] How do I print a double value with full precision using cout?

查看:552
本文介绍了如何使用cout以全精度打印双精度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我得到了我的最后一个问题的答案(I不知道为什么我没有想到的)。我在使用 cout 打印一个 double ,当我不期待它。如何让 cout 使用全精度打印 double

So I've gotten the answer to my last question (I don't know why I didn't think of that). 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 used the std::fixed format specifier.

double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;

您可以 #include< limits> 以获得float或double的最大精度。

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: " << fixed << d << endl;

这篇关于如何使用cout以全精度打印双精度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆