Controlling precision of floating-point values : float output « Data Type « C++






Controlling precision of floating-point values

   
#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::setiosflags;
using std::setprecision;

#include <cmath>

int main()
{
   double root2 = sqrt( 2.0 );
   int places;

   cout << setiosflags( ios::fixed) << endl;

   for ( places = 0; places <= 9; places++ ) {
      cout.precision( places );
      cout << root2 << '\n';
   }

   for ( places = 0; places <= 9; places++ )
      cout << setprecision( places ) << root2 << '\n';

   return 0;
}
  
    
    
  








Related examples in the same category

1.Set precision for float number
2.Output float number with fixed flag
3.Output float type number with scientific format
4.Change cout width and precision for string and float number output
5.Output float in default floating-point format
6.Floating-point values in system default, scientific, and fixed formats.