Home C++ Iostream Functions fixed C++ fixed Manipulator Definition In C++, fixed makes a stream print floating-point numbers in normal decimal form, never in scientific notation. It also changes the meaning of setprecision: the number then counts the digits after the decimal point.
That is why fixed with setprecision(2) is the usual way to show money with two decimals. The setting stays on until changed. fixed comes from <ios>, and setprecision comes from <iomanip>.
This is a manipulator . You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).
Syntax Put std::fixed into the stream and then set the precision.
#include <iomanip>
std::cout << std::fixed << std::setprecision(2);
std::cout << 3.14159; // 3.14
std::cout << 2.0; // 2.00 Call What it does std::cout << std::fixedNormal decimal form. The default precision is 6 digits after the point. std::cout << std::fixed << std::setprecision(2)Exactly two digits after the point. std::cout << std::defaultfloatGoes back to the default style.
Numbers are rounded when printed. The stored value in the variable does not change.
Library declarations ios_base& fixed(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << fixed. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after fixed.
Important
Without fixed, setprecision(2) counts all significant digits, so 3.14159 prints as 3.1. With fixed, it counts digits after the point: 3.14. Both fixed and the precision stay on until you change them.
C++ Examples 1. Two decimals 2. With and without 3. Money Example 1 — Print Exactly Two Decimals Whole numbers also get their zeros.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::fixed << std::setprecision(2);
std::cout << 3.14159 << '\n';
std::cout << 2.0 << '\n';
return 0;
} How It Works fixed turns on normal decimal form, and setprecision(2) asks for two digits after the point.3.14159 is rounded to 3.14.2.0 gets a zero added, so it prints as 2.00. Without fixed it would print as 2.Example 2 — See What fixed Changes A big number prints in scientific form by default.
#include <iomanip>
#include <iostream>
int main() {
double big = 1234567.0;
std::cout << big << '\n';
std::cout << std::fixed << std::setprecision(2) << big << '\n';
return 0;
} How It Works By default a double shows six significant digits, so 1234567.0 is shortened to 1.23457e+06. fixed stops scientific notation. All digits before the point are always shown.setprecision(2) then adds two digits after the point, so the second line is 1234567.00.Example 3 — Show a Price Total A price calculation printed like money.
#include <iomanip>
#include <iostream>
int main() {
double price = 19.99;
int count = 3;
std::cout << std::fixed << std::setprecision(2);
std::cout << "Total: $" << price * count << '\n';
std::cout << "Tax: $" << price * count * 0.1 << '\n';
return 0;
} How It Works price * count is 59.97. With two decimals fixed, it prints exactly like that.The tax is 5.997. Printing rounds it to two decimals: 6.00. The variable itself still holds 5.997. Only the printed text is rounded. Frequently Asked Questions What does fixed do in C++? It prints floating-point numbers in plain decimal form (no scientific notation) and makes setprecision count digits after the decimal point.
How do I print a double with 2 decimal places? Use cout << fixed << setprecision(2) << value. It needs #include <iomanip>.
Does fixed round the number? It rounds only the printed text. The variable keeps its full value.
What is the difference between fixed and scientific? fixed prints like 1234.50. scientific prints like 1.234500e+03.
How do I turn fixed off? Use std::defaultfloat, or cout.unsetf(std::ios::floatfield).
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion