HOWTO · C++
How to Use setprecision in C++
Learn how the C++ setprecision stream manipulator controls significant digits, how std::fixed sets decimal places, and how to align formatted values in columns.
On this page
In C++, std::setprecision(n) sets the precision of subsequent floating-point values written to a stream. With the default floating-point format, n is the number of significant digits, not the number of digits after the decimal point. Add std::fixed when you need a fixed number of digits after the decimal point.
setprecision is a stream manipulator declared in <iomanip>. The examples below use std::cout; the same formatting state rules apply to other output streams.
How setprecision controls precision
Without std::fixed or std::scientific, floating-point output uses the default format. In this format, the precision counts significant digits. The stream rounds the displayed value to that many digits, so leading zeroes after the decimal point do not count as significant digits. The default format can choose ordinary or scientific notation according to the value and precision; setting a precision does not mean that scientific notation starts at a simple fixed digit count.
Run this example:
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setprecision(5);
std::cout << 123.456 << '\n';
std::cout << 0.0123456 << '\n';
std::cout << 1234567.0 << '\n';
}
123.46
0.012346
1.2346e+06
The first value is rounded to five significant digits. In the second value, leading zeroes do not use the precision allowance. The large third value may be displayed in scientific notation by the default format. To request scientific notation explicitly, apply std::scientific; it uses the precision as the number of digits after the decimal point. std::defaultfloat restores the default format.
These manipulators change the stream’s formatting state. A precision set with std::setprecision remains in effect for later floating-point insertions on that stream, until another precision is set. Include <iomanip> in a program that uses std::setprecision.
Set a fixed number of decimal places with std::fixed
Combine std::fixed with std::setprecision(n) to print exactly n digits after the decimal point. This is useful for quantities such as prices or measurements that should have a consistent number of decimal places. The stream rounds the displayed value as needed and pads with trailing zeroes when the value has fewer fractional digits.
#include <iomanip>
#include <iostream>
int main() {
const double price = 12.5;
const double measurement = 3.14159;
std::cout << std::fixed << std::setprecision(2);
std::cout << price << '\n';
std::cout << measurement << '\n';
}
12.50
3.14
std::fixed affects subsequent floating-point output on that stream, and std::setprecision(2) remains active until changed. To return to the default floating-point format, insert std::defaultfloat; set the desired precision again if needed for later output.
Align formatted values in columns
For a simple table, combine std::setw, std::right, std::setfill, std::fixed, and std::setprecision. The width set by std::setw(n) applies only to the next inserted value, so call it for each column entry. std::right places any padding before the value, while std::setfill chooses the padding character. With fixed decimal places, values of different magnitudes share the same fractional width, which makes their decimal points line up when right-aligned in equal-width fields.
#include <iomanip>
#include <iostream>
int main() {
const double values[] = {123.45, 2.5, 89.0};
std::cout << std::fixed << std::setprecision(2)
<< std::right << std::setfill(' ');
for (double value : values) {
std::cout << std::setw(10) << value << '\n';
}
}
123.45
2.50
89.00
std::right, the fill character, fixed formatting, and precision remain part of the stream state for later insertions. Width is the exception: it is consumed by the next formatted insertion, so apply std::setw again for every value. Choose a field width large enough for the widest value; a field does not truncate a value that exceeds its width.