Demonstrate setting the precision. - C++ File Stream

C++ examples for File Stream:cout

Description

Demonstrate setting the precision.

Demo Code

#include <iostream>
using namespace std;
int main()/*  w  ww .j a  va 2s  . c o m*/
{
   double f = 123456.123456789;
   cout << "Using default numeric format.\n";
   cout << "f with default precision: " << f << "\n\n";
   cout << "Setting the precision to 9.\n";
   cout.precision(9);
   cout << "f with precision of 9: " << f << "\n\n";
   cout << "Switching to fixed-point format.\n";
   cout.setf(ios::fixed, ios::floatfield);
   cout << "f with precision of 9 in fixed-point: " << f << "\n\n";
   // Now, display two decimal places.
   cout << "Display two decimal places in all cases: ";
   cout.precision(2);
   cout << 12.456 << " " << 10.0 << " " << 19.1 << endl;
   return 0;
}

Result


Related Tutorials