CKTIS / C++
std::locale — localization rules
std::locale from <locale> holds a collection of facets controlling rules such as numeric notation, character classification, and text collation. Assign it to a stream with imbue() to choose that stream's rules explicitly.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <locale>
std::locale loc = std::locale::classic();
stream.imbue(loc);std::locale from <locale> represents a collection of facets: objects providing localization rules. Categories cover numbers, characters, text collation, dates and times, money, and messages. Merely constructing an object does not change an existing stream's settings.
std::locale() copies the current global C++ locale, whereas std::locale::classic() provides the unchanging rules of the classic "C" locale. The named constructor selects a named locale; beyond "C" and "", supported names are implementation-defined. An invalid name throws std::runtime_error, so do not assume a particular country's locale is available.
Call stream.imbue(loc) to assign a locale to that stream. In the example, std::locale::classic() ensures a decimal point and no digit grouping; std::fixed and std::setprecision(2) specify two digits after the point. The result is 1234.50 followed by a newline, without depending on installed named locales.
std::locale::global(loc) changes the locale used by future calls to std::locale(); for a named object, it also calls setlocale(LC_ALL, loc.name().c_str()). This is a global change, so prefer imbue() for isolated formatting. This entry targets C++23; the cited working draft continues to evolve, and the example requires local verification—production runtime compatibility is not attested.
Examples
#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
int main() {
std::ostringstream out;
out.imbue(std::locale::classic());
out << std::fixed << std::setprecision(2) << 1234.5;
std::cout << out.str() << '\n';
}
Result
1234.50
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.