HOWTO · C++
How to Print Numbers With Specified Decimal Points in C++
Learn how to print C++ floating-point values with a chosen number of decimal places using std::fixed and std::setprecision, align values with stream manipulators, and format output with printf.
On this page
To print a floating-point value with a chosen number of digits after the decimal point, write std::fixed followed by std::setprecision(n) to std::cout. For example, std::fixed << std::setprecision(2) formats values with two digits after the decimal point. Include <iostream> for output and <iomanip> for the manipulators.
Using std::fixed and std::setprecision
std::fixed selects fixed-point notation. With that format active, std::setprecision(n) sets the number of digits after the decimal point, so values are rounded for display and padded with trailing zeros when needed. Without std::fixed, std::setprecision(n) instead controls the number of significant digits. See the setprecision guide for more detail.
Compile and run this complete program:
#include <iomanip>
#include <iostream>
int main() {
const double values[] = {3.14159, 2.5, 0.004};
std::cout << std::fixed << std::setprecision(2);
for (double value : values) {
std::cout << value << '\n';
}
}
3.14
2.50
0.00
The first value is rounded for display, the second is shown with a trailing fractional digit, and the third is formatted to the same two-place precision. Formatting changes the displayed representation; it does not change the stored values or make floating-point arithmetic exact. To show three digits after the decimal point, use std::setprecision(3).
The stream keeps std::fixed and the selected precision for later floating-point insertions. Set a different precision when subsequent values need a different display. The manipulators are declared in <iomanip>.
Aligning Values with std::setw and std::setfill
For columns, std::setw(width) sets a minimum field width for the next inserted value. It does not truncate a value that needs more space, and the width resets after that insertion. std::setfill(ch) selects the padding character and remains active until changed. The default alignment for numbers is right-aligned.
This complete example uses dots as padding and keeps each formatted number in an eight-character field:
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::fixed << std::setprecision(2) << std::setfill('.')
<< std::setw(8) << 12.5 << '\n'
<< std::setw(8) << 3.75 << '\n';
}
...12.50
....3.75
Apply std::setw again for every value that needs a field width; changing the fill character does not set a width. Choose a width large enough for the sign, digits, decimal point, and fractional digits you expect.
Formatting with printf
C++ programs can also use printf from <cstdio>. In its format string, %.2f writes a floating-point value with two digits after the decimal point; change 2 to choose another count.
#include <cstdio>
int main() {
const double value = 1.41421;
std::printf("Number with two decimal places: %.2f\n", value);
}
Number with two decimal places: 1.41
For printf, %f is the floating-point conversion for a double argument. The format specifier and argument type must match; an incorrect pairing can produce undefined behavior. This example is a complete C++ program and needs no stream headers.
Choosing Between C++ Streams and printf
Use std::cout with std::fixed and std::setprecision when you want C++ stream formatting that composes with other stream insertions and preserves the type of each value. Use printf when working with existing printf-style format strings or when that syntax is more convenient. Both are valid in C++.
For fixed decimal places, remember to combine std::fixed with std::setprecision; the precision manipulator alone has a different meaning in the default stream format. Use std::setw and std::setfill when columns need padding, and reapply std::setw before each value because its width applies once.