14

I want to output the value of a double in it's full precision. However, when using the cout function, it only displays the first 6 digits even though there is around 15-16 digits of precision.

How do I get my program to display the entire value, including the magnitude (power) component?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Faken
  • 11,352
  • 17
  • 59
  • 74

3 Answers3

13

Use the setprecision() manipulator:

http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

You can also force scientific notation with the scientific manipulator:

http://www.cplusplus.com/reference/iostream/manipulators/scientific/

cout << scientific << setprecision(15) << my_number << endl;
Amber
  • 507,862
  • 82
  • 626
  • 550
9

you could use something like this :

#include <iomanip>

cout << setprecision (9) << double_value << endl;

more iomanipulators, here

Indy9000
  • 8,651
  • 2
  • 32
  • 37
  • what if you wanted it to be fully precise. but obviously turn the number such as 7.3333333 to lets say 7.33 – Angel.King.47 Aug 05 '09 at 07:36
  • The is no equivalent of %g format of printf. And that format doesn't give you what some other languages give: the shortest string which reads back to the original number. – AProgrammer Aug 05 '09 at 07:43
7

You're looking for setprecision (code taken from link):

int main () {
  double f =3.14159;
  cout << setprecision(15) << f << endl;
  return 0;
}
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • 1
    Just in case you missed it down below, you can also use the scientific io-manipulator to force scientific notation if you desire. – Amber Aug 05 '09 at 07:43