CKTIS / C++
std::fixed — fixed-point stream formatting
std::fixed selects fixed-point formatting for floating-point values in a stream. Together with std::setprecision(n), it specifies the number of digits after the decimal separator, and the setting remains active for subsequent operations.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
std::ios_base& std::fixed(std::ios_base& str);
stream << std::fixed << std::setprecision(n) << value;std::fixed is a manipulator declared in <ios>. In stream << std::fixed, it selects fixed-point notation: finite floating-point values are printed without an exponent. It does not change a number's value or type; it affects its textual representation.
Specifically, it calls str.setf(std::ios_base::fixed, std::ios_base::floatfield) and returns str. This replaces the previous floating-point format selection. The setting belongs to the stream and applies to subsequent insertions until changed.
With std::fixed, precision means the number of digits after the decimal separator, not significant digits. Thus, std::setprecision(2) from <iomanip> produces 12.50 for 12.5 and 3.00 for 3.0. Formatting rounds the output to the requested precision rather than simply truncating digits. The separator depends on the stream's locale; the example explicitly selects std::locale::classic().
std::defaultfloat clears the format selection but does not reset precision. The retained precision of 2 now means two significant digits. At this precision, 12.5 is exactly halfway between 12 and 13, and std::locale::classic() does not determine which way the tie is rounded. The final line was 12 in the tested environment, but that result is not guaranteed in other environments. When temporarily changing someone else's stream, save and restore both its flags and its precision.
Examples
#include <iomanip>
#include <ios>
#include <iostream>
#include <locale>
int main() {
std::cout.imbue(std::locale::classic());
std::cout << std::fixed << std::setprecision(2);
std::cout << 12.5 << '\n';
std::cout << 3.0 << '\n';
std::cout << std::defaultfloat << 12.5 << '\n';
}
Result
12.50 3.00 12
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.