C++ fixed Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

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.

C++
#include <iomanip>

std::cout << std::fixed << std::setprecision(2);
std::cout << 3.14159;     // 3.14
std::cout << 2.0;         // 2.00
CallWhat 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
C++
ios_base& fixed(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << fixed. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after fixed.

C++ Examples

Example 1 — Print Exactly Two Decimals

Whole numbers also get their zeros.

C++
#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

  1. fixed turns on normal decimal form, and setprecision(2) asks for two digits after the point.
  2. 3.14159 is rounded to 3.14.
  3. 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.

C++
#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

  1. By default a double shows six significant digits, so 1234567.0 is shortened to 1.23457e+06.
  2. fixed stops scientific notation. All digits before the point are always shown.
  3. 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.

C++
#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

  1. price * count is 59.97. With two decimals fixed, it prints exactly like that.
  2. The tax is 5.997. Printing rounds it to two decimals: 6.00.
  3. The variable itself still holds 5.997. Only the printed text is rounded.

Frequently Asked Questions

It prints floating-point numbers in plain decimal form (no scientific notation) and makes setprecision count digits after the decimal point.
Use cout << fixed << setprecision(2) << value. It needs #include <iomanip>.
It rounds only the printed text. The variable keeps its full value.
fixed prints like 1234.50. scientific prints like 1.234500e+03.
Use std::defaultfloat, or cout.unsetf(std::ios::floatfield).

Next: hex

Read and print integers in base 16.

hex tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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