<<< Default: fixed-point notation     Index     Floating-point format and precision example >>>

13. Floating-point Precision

  • Floating-point values are printed with exact fraction part, as specified by the precision.

  • In default notation format, the precision specifies max number of meaningful digits to display both before and after the decimal point.

  • The default precision is 6 digits.

  • This means that the value is rounded to the best approximation while using 6 digits.

  • If necessary, the default format may change to scientific notation to preserve most accurate representation.

  • To improve data accuracy display, the precision can be changed by setprecision(n) manipulator, or by calling precision( ) member function of the stream:

    
    #include <iostream>
    int main()
    {
      const double PI = 3.1415926;
      std::cout.precision( 7 );
      std::cout << PI << '\n';
      return 0;
    }
    /*
    Output: 3.141593
    */
    
    

<<< Default: fixed-point notation     Index     Floating-point format and precision example >>>