+ 7
When doubles are printed, why do only 6 digits show on the output?
just curious since I thought doubles could hold large values, but show only 6 by default eg. double number = 1.231231; cout << number; [output:] 1.23123 //missing the last digit
7 Respostas
+ 3
6 digits of accuracy is standard for stream formatting in C++. Standard library includes many manipulators to control formatting, including setprecision that changes number of decimals printed:
http://www.cplusplus.com/reference/library/manipulators/
+ 7
But doesn't set precision only set values after the decimal point? What if I just wanted the default formatting to be 10 digits and not 10 decimal spaces?
+ 7
You may also use:
cout.setf(ios::fixed);
//To ensure that numbers are printed only in fixed notation, not as 10^ something...
cout.precision(10);
//Sets precision after decimal point...
cout<<number;
//outputs 1.2312310000
//Changes default precision...
+ 5
Thanks for the info! Is there a way to change the default formatting of digits of accuracy if I wanted to set it to 10 digits instead of 6?
+ 3
try setwidth
+ 2
Yes, as I mentioned use setprecision:
cout << setprecision(10) << number;
+ 1
that is interesting, Yes a double should be able to hold that value. It just might be something to do with the sololearn compiler. Try it on code::blocks and see if it does the same.