ios_base::precision
streamsize precision ( ) const;
streamsize precision ( streamsize prec );
ios_base
  cplusplus.com  

Get/Set floating-point decimal precision.
  The first syntax returns the value of the current precision.
  The second syntax sets a new value for the floating-point precision.
  The precision determines the maximum number of digits that shall be output on insertion operations to express floating-point values, counting both the digits before and after the decimal point.

Parameters.

prec
New value for the stream's floating-point precision. This is an integer value of type streamsize.

Return Value.
  The format flags of the stream before the call.

Example.

// modify precision
#include <iostream>
using namespace std;

int main () {
  double f = 3.14159;
  cout.precision(5);
  cout << f << endl;
  cout.precision(10);
  cout << f << endl;
  return 0;
}
The execution of this example shall display:
  3.1416
  3.14159
Notice how the first number output is only 5 digits long, while the second displays 6, but not more. The precision forces the maximum number of digits to be displayed.

See also.
  flags, width
  ios_base class


© The C++ Resources Network, 2001