Set the precision to 9 : double output « Data Type « C++






Set the precision to 9

  
#include <iostream>

using namespace std;

int main()
{
  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;
}
  
    
  








Related examples in the same category

1.Set showpos flag for double
2.Set fixed flag for double value
3.Set the uppercase flag for double value
4.Set scientific flag when outputing double
5.Scientific format with precision of 7
6.Fixed format with precision of 7
7.double: Formatting aligns columns, pads blank spaces with '0' character, and controls precision of answer.