Wednesday, August 20, 2014

Output double/float numbers with precision

Sometimes we require to output numbers rounded to some exact precision. There are a few ways of doing them. If you are using scanf/printf you can do it in this way. Suppose we need to  output to 7th precision,

double a;
printf("%.7f",a);

This means output a rounded to 7th precision.

 If you need to use cin and cout here is how you should do it. Include iomanip  ( #include <iomanip> )
then before couting write this
cout<< fixed << setprecision (7);
cout<< a<< endl;




No comments:

Post a Comment