tags:

views:

81

answers:

3

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?

Thanks in advance,

-Faken

+2  A: 

you could use something like this :

#include <iomanip>

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

more iomanipulators, here

Indeera
what if you wanted it to be fully precise. but obviously turn the number such as 7.3333333 to lets say 7.33
Shahmir Javaid
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
+2  A: 

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
+5  A: 

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

int main () {
  double f =3.14159;
  cout << setprecision(15) << f << endl;
  return 0;
}
280Z28
Ahh thank you. I love this website, you can get quick and concise answers to your questions any time of the day!
Faken
Just in case you missed it down below, you can also use the scientific io-manipulator to force scientific notation if you desire.
Amber