Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Formatting numbers with cout.setf and setprecision(2)

Set `std::cout` to fixed-point notation with two digits after the decimal separator. Learn why the `floatfield` mask matters and how to restore the previous format.

Version: ISO C++23; implementation support may vary

Syntax

// ISO C++23; implementation support may vary
#include <iostream>
#include <iomanip>

// Inside a function, assuming standard stream formatting:
std::cout.setf(std::ios::fixed, std::ios::floatfield);
std::cout << std::setprecision(2) << 12.5 << '\n';

// Equivalent manipulator form:
std::cout << std::fixed << std::setprecision(2) << 12.5 << '\n';

std::cout is the standard output stream from <iostream>. The spelling cout.setf(ios::fixed) assumes the names cout and ios from std are available unqualified, for example through using declarations. std::ios is an alias for std::basic_ios<char>; the setf member and the flags come from std::ios_base. The dot selects a member of the stream, and :: qualifies a name by its scope.

The one-argument setf(std::ios::fixed) sets the fixed bit without clearing scientific. If both bits are set, standard formatting uses hexadecimal rather than decimal fixed-point notation. std::cout.setf(std::ios::fixed, std::ios::floatfield) clears the bits covered by the floatfield mask and then sets only fixed within it, leaving other flags alone. Both overloads return the previous flags, not the stream. std::cout << std::fixed has the same effect as the masked call.

std::cout << std::setprecision(2) uses the manipulator from <iomanip> to set the stream's precision. In fixed mode it means two digits after the decimal separator, including trailing zeros; in defaultfloat mode it means precision in significant digits, not decimal places. Formatting changes the text, not the stored value. The mode and precision stay set on that stream until changed. For a temporary change, save flags() and precision() and restore both afterwards. The stream's locale determines the decimal separator.

Examples

#include <iomanip>
#include <iostream>
#include <locale>

int main() {
    std::cout.imbue(std::locale::classic());
    const auto saved_flags = std::cout.flags();
    const auto saved_precision = std::cout.precision();

    std::cout << std::scientific;
    std::cout.setf(std::ios::fixed, std::ios::floatfield);
    std::cout << std::setprecision(2) << 12.5 << '\n';

    std::cout << std::scientific;
    std::cout << std::fixed << std::setprecision(2) << 3.0 << '\n';

    std::cout.flags(saved_flags);
    std::cout.precision(saved_precision);
    std::cout << 12.5 << '\n';
}

Result

12.50
3.00
12.5

The first two lines switch from `scientific` to `fixed`, first with masked `setf` and then with `std::fixed`. Restoring the flags and precision brings back the initial format on the third line. The classic locale gives a decimal point.

Related lessons

  • C++ · Lesson 22 · Intermediate · Version: 2

Sources