Cpp - Output Number Precision


Both the manipulator setprecision() and the method precision() can redefine precision.

cout << setprecision(3);  // Precision: 3 
// or:  
cout  << 12.34;           // Output: 12.3 

The header file iomanip must be included when using the manipulator setprecision().

This applies to all standard manipulators called with at least one argument.

The manipulator showpoint outputs the decimal point and trailing zeroes.

The number of digits being output (e.g. 6) equals the current precision.

cout << showpoint << 1.0; // Output: 1.00000 

You can use the fixed manipulator with the precision defining the number of decimal places.

The default value of 6 is assumed.

cout << fixed << 66.0;   // Output: 66.000000 

You can use the scientific manipulator to specify that floating-point numbers are output as exponential expressions.

Related Topic