std::money_put::put,do_put(3)
β¨ NAME β¨
okay so welcome to std::money_put::put,do_put(3) from the C++ Standard Libary manual β yes bestie, thatβs the name on the tin, and weβre keeping it. This page is literally about std::money_put::put and do_put: std::money_put::put,do_put πΈβ¨
π¦ Synopsis π¦
Defined in header <locale> β iconic. The public and protected signatures are giving overload family realness:
Defined in header <locale>
public:
iter_type put( iter_type out, bool intl, std::ios_base& f,
(1)
char_type fill, long double quant ) const;
iter_type put( iter_type out, bool intl, std::ios_base& f,
(2)
char_type fill, const string_type& quant ) const;
protected:
virtual iter_type do_put( iter_type out, bool intl, std::ios_base&
str, (3)
char_type fill, long double units ) const;
virtual iter_type do_put( iter_type out, bool intl, std::ios_base&
str, (4)
char_type fill, const string_type& digits )
const;
Formats monetary value and writes the result to output stream. Thatβs the whole mood, but the details are where the slay lives:
- 1, 2) β public member functions; they call the member function
do_putof the most derived class. - 3) β the numeric argument
unitsis converted to a wide character string as if byct.widen(buf1, buf1 + std::sprintf(buf1, "%.0Lf", units), buf2), wherectis thestd::ctypefacet imbued instr.getloc()andbuf1andbuf2are sufficiently large character buffers. The resulting character stringbuf2is processed, formatted, and output tooutas described below. - 4) β from the string argument
digits, only the optional leading minus sign (as determined by comparing toct.widen('-'), wherectis thestd::ctypefacet imbued instr.getloc()) and the immediately following digit characters (as classified byct) are taken as the character sequence to be processed, formatted, and output tooutas described below.
Given the character sequence from the previous steps, if the first character equals ct.widen('-'), calls mp.neg_format() to obtain the formatting pattern, otherwise calls mp.pos_format(), where mp is the std::moneypunct<CharT, intl> facet imbued in str.getloc().
Thousands separator and decimal point characters are inserted as required by mp.grouping(), mp.frac_digits(), mp.decimal_point(), and mp.thousands_sep(), and the resulting string is placed in the output sequence where value appears in the formatting pattern.
If str.flags() & str.showbase is non-zero (the std::showbase manipulator was used), then the currency symbol or string is generated by calling mp.curr_symbol() and placed in the output sequence where symbol appears in the formatting pattern.
If mp.positive_sign() (in case positive format pattern is used) or mp.negative_sign() (in case negative format pattern is used) returns a string with more than one character, the first character returned is placed in the output sequence where sign appears in the formatting pattern, and the rest of the characters are placed after all other characters, for example, formatting pattern {sign, value, space, symbol} with units 123 and negative_sign of "-" may result in "-1.23 β¬", while negative_sign of "()" would generate "(1.23 β¬)".
If the number of characters generated for the specified format is less than the value returned by str.width(), then copies of fill are inserted to bring the total length of the output sequence to exactly str.width(), as follows:
- If
str.flags() & str.adjustfieldequalsstr.internal, the fill characters are inserted wherenoneorspaceappears in the formatting pattern. - Otherwise, if
str.flags() & str.adjustfieldequalsstr.left, the copies offillare appended after all other characters. - Otherwise, the fill characters are placed before all other characters.
In the end, calls str.width(0) to cancel the effects of any std::setw.
π Return value π
An iterator pointing immediately after the last character produced. periodt. π«Ά
πΈ Notes πΈ
The currency units are assumed to be the smallest non-fractional units of the currency: cents in the U.S, yen in Japan. iykyk π΄
π Example π
// Run this code
#include <iomanip>
#include <iostream>
#include <locale>
struct my_punct : std::moneypunct_byname<char, false>
{
my_punct(const char* name) : moneypunct_byname(name) {}
string_type do_negative_sign() const { return "()"; }
};
int main()
{
std::locale loc("ru_RU.utf8");
std::cout.imbue(loc);
long double units = -123.45;
std::cout << "In Russian locale, " << units << " prints as " << std::showbase;
// note, the following is equivalent to simply std::put_money(units)
std::use_facet<std::money_put<char>>(loc).put(
{std::cout}, false, std::cout, std::cout.fill(), units);
std::cout << '\n';
std::cout.imbue(std::locale(std::cout.getloc(), new my_punct("ru_RU.utf8")));
std::cout << "With negative_sign set to \"()\", it prints as ";
std::use_facet<std::money_put<char>>(loc).put(
{std::cout}, false, std::cout, std::cout.fill(), units);
std::cout << '\n';
}
π Output π
In Russian locale, -123,45 prints as -1.23 ΡΡΠ±
With negative_sign set to "()", it prints as (1.23 ΡΡΠ±)
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 328 β applied to C++98; the behavior as published was that the format string used for
std::sprintfwas"%.01f"; correct behavior is"%.0Lf". not me keeping the glow-up receipts π
π See also π
moneypunctβ defines monetary formatting parameters used bystd::money_getandstd::money_put(class template)money_getβ parses and constructs a monetary value from an input character sequence (class template)put_money(C++11) β formats and outputs a monetary value (function template)
thatβs the whole page, bestie! source/version: http://cppreference.com, dated 2024.06.10 β std::money_put::put,do_put(3) signing off π
β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms