setprecision

Set and return the current format precision.

smanip<int> setprecision(int)

Remarks

Use the manipulator setprecision() directly in the output stream with floating point numbers to limit the number of digits. You may use setprecision() with scientific or non-scientific floating point numbers.

With the flag ios::floatfield set, the number in setprecision refers to the total number of significant digits generated. If the settings are for either ios::scientific or ios::fixed then the setprecision refers to the number of digits after the decimal place.

This means that ios::scientific will have one more significant digit than ios::floatfield, and ios::fixed will have a varying number of digits.

Returns a smanip type, which is an implementation defined type.

See Also

ios_base::setf(), ios_base::precision()

Listing: Example of setprecision() usage:
#include <iostream>
#include <iomanip>

int main()
{
using namespace std;
   cout << "Original: " << 321.123456 <<    endl;
   cout << "Precision set: " << setprecision(8)
      << 321.123456 <<    endl;
   return 0;
}

Result:

  Original:   321.123
  Precision set:   321.12346