C++ iomanip setprecision() Function
Example
Print a decimal number with different precision:
double pi = 3.14159;
cout << setprecision(4) << pi << "\n";
cout << fixed << setprecision(2) << pi;
Definition and Usage
The setprecision() function sets how many digits are printed for decimal numbers.
Without fixed, the number is the total number of digits. With fixed, it is the number of digits after the decimal point. The number is rounded, not cut off.
The setprecision function is defined in the <iomanip> header file.
Tip: Learn more in the Decimal Precision chapter.
Syntax
setprecision(int n)
Parameter Values
| Parameter | Description |
|---|---|
| n | Required. The number of digits to print. |
Technical Details
| Returns: | A manipulator that you use with <<. It changes all the decimal numbers printed after it. |
|---|