Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
IOstream Library
manipulators
classes:
filebuf
fstream
ifstream
ios
iostream
ios_base
istream
istringstream
ofstream
ostream
ostringstream
streambuf
stringbuf
stringstream
objects:
cerr
cin
clog
cout
types:
fpos
streamoff
streampos
streamsize
ios_base
ios_base::ios_base
ios_base::~ios_base
member classes:
ios_base::failure
ios_base::Init
member functions:
ios_base::flags
ios_base::getloc
ios_base::imbue
ios_base::iword
ios_base::precision
ios_base::pword
ios_base::register_callback
ios_base::setf
ios_base::sync_with_stdio
ios_base::unsetf
ios_base::width
ios_base::xalloc
member types:
ios_base::event
ios_base::event_callback
ios_base::fmtflags
ios_base::iostate
ios_base::openmode
ios_base::seekdir


ios_base::precision

public member function
streamsize precision ( ) const;
streamsize precision ( streamsize prec );

Get/Set floating-point decimal precision

The first syntax returns the value of the current floating-point precision field for the stream.
The second syntax also sets it to a new value.

The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is neither fixed nor scientific):

  • On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
  • In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case.

This decimal precision can also be modified using the parameterized manipulator setprecision.

Parameters

prec
New value for the floating-point precision. This is an integral value of type streamsize.

Return Value

The value set as precision for the stream before the call.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// modify precision
#include <iostream>
using namespace std;
int main () {
  double f = 3.14159;
  cout.setf(0,ios::floatfield);            // floatfield not set
  cout.precision(5);
  cout << f << endl;
  cout.precision(10);
  cout << f << endl;
  cout.setf(ios::fixed,ios::floatfield);   // floatfield set to fixed
  cout << f << endl;
  return 0;
}


The execution of this example displays something similar to:
3.1416
3.14159
3.1415900000

Notice how the first number written is just 5 digits long, while the second is 6, but not more, even though the stream's precision is now 10. That is because precision with the default floatfield only specifies the maximum number of digits to be displayed, but not the minimum.
The third number printed displays 10 digits after de decimal point because the floatfield format flag is in this case set to fixed.

See also