When doubles are printed, why do only 6 digits show on the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

31st Mar 2017, 10:57 PM
Aeedoom Aeedad
Aeedoom Aeedad - avatar
7 Answers
+ 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/
1st Apr 2017, 1:16 AM
Igor B
Igor B - avatar
+ 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?
1st Apr 2017, 5:07 AM
Aeedoom Aeedad
Aeedoom Aeedad - avatar
+ 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...
10th Apr 2017, 8:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 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?
1st Apr 2017, 2:32 AM
Aeedoom Aeedad
Aeedoom Aeedad - avatar
+ 3
try setwidth
1st Apr 2017, 6:52 AM
Igor B
Igor B - avatar
+ 2
Yes, as I mentioned use setprecision: cout << setprecision(10) << number;
1st Apr 2017, 4:53 AM
Igor B
Igor B - avatar
+ 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.
1st Apr 2017, 12:05 AM
Jason Hoffman
Jason Hoffman - avatar