I have the following piece of code written in c++ and compiled by g++ 4.8.
double x = 0.123456789;
cout << x << endl;
I don't understand why I only get the output
0.1234567
even I define x as long double x
. It probably a quite naive question, but can any one give me some hits?
This page has all you need, namely that you should use the std::setprecision
stream manipulator.
double x = 0.123456789;
std::cout << std::setprecision(10) << x << std::endl;
Use the precision
function or the setprecision
manipulator to set the number of significant figures (or decimal places, if you also use fixed
).
cout.precision(10); // 10 significant figures
or
cout << setprecision(10); // also 10 significant figures
or
cout << fixed << setprecision(10); // always fixed-point format, 10 decimal places
By default, the precision is 6.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.