Controlling precision of floating-point values : cout float « Development « C++ Tutorial






#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
   double root2 = sqrt( 2.0 );
   int places;

   cout << setiosflags( ios::fixed)
        << "Square root of 2 with precisions 0-9.\n"
        << "Precision set by the "
        << "precision member function:" << endl;

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

   cout << "\nPrecision set by the "
        << "setprecision manipulator:\n";

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

   return 0;
}








5.6.cout float
5.6.1.Displaying floating-point values in system default, scientific and fixed formats
5.6.2.ios::right|ios::scientific|ios::showpoint
5.6.3.Displaying numbers in scientific notation
5.6.4.Normal (default) mode; only show 5 digits, including both sides of decimal point
5.6.5.Controlling precision of floating-point values