[英]How do I make “std::cout << 123456789.12” print “123456789.12”?
How do I make 我该怎么做
std::cout << 123456789.12
print this: 打印这个:
123456789.12
It always prints this: 它始终打印这个:
1.23457e+008
I know that I have to play with the flags, but I cant quite figure out the right combination. 我知道我必须玩旗帜,但我无法弄清楚正确的组合。 If I set the fixed flag, it prints
如果我设置固定标志,则打印
123456789.120000
You can use: 您可以使用:
#include <iostream>
#include <limits>
using namespace std;
int main() {
double c = 123456789.12;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << c << endl;
return 0;
}
Basically the limits
package has traits for all the build-in types. 基本上,
limits
包具有所有内置类型的特征。 One of the traits for floating point numbers ( float/double/long double
) is the digits10
attribute . 浮点数(
float/double/long double
)的特征之一是digits10
属性 。 This defines the accuracy of a floating point number in base 10. 这定义了基数10中浮点数的准确性。
See it live: http://ideone.com/Ity9m7 现场观看: http : //ideone.com/Ity9m7
To read on, check out another similar question: How do I print a double value with full precision using cout? 要继续阅读,请查看另一个类似的问题: 如何使用cout以完全精度打印double值?
You can use boost::lexical_cast as follow: 你可以使用boost :: lexical_cast如下:
#include <boost/lexical_cast.hpp>
std::cout << boost::lexical_cast<std::string>(123456789.12);
more info can be found in http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html 更多信息可以在http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.