std::moneyp..._neg_format(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

Hey bestie, gather ’round: today we’re reading std::moneypunct::pos_format, do_pos_format, neg_format, and do_neg_format from the C++ Standard Libary (yes, that’s literally how the header spells it 😭). Manual name near the top: std::moneyp..._neg_format(3). Money formatting lore incoming πŸ’Έβœ¨

🌸 NAME 🌸

std::moneypunct::pos_format,do_pos_format,neg_format,do_neg_format β€” and yep, the original repeats the whole thing: std::moneypunct::pos_format,do_pos_format,neg_format,do_neg_format. It’s giving β€œsay it twice so you remember it” energy πŸ’…


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <locale>:

public:                                (1)
pattern pos_format() const;
public:                                (2)
pattern neg_format() const;
protected:                             (3)
virtual pattern do_pos_format() const;
protected:                             (4)
virtual pattern do_neg_format() const;
  1. Public member function. It calls the member function do_pos_format of the most derived class.
  2. Public member function. It calls the member function do_neg_format of the most derived class.
  3. Returns the format structure (of type std::money_base::format) which describes the formatting of positive monetary values.
  4. Returns the format structure (of type std::money_base::format) which describes the formatting of negative monetary values.

The standard specializations of std::moneypunct return the pattern {symbol, sign, none, value}. Iconic, honestly πŸ‘‘


πŸ’° Return value πŸ’°

The object of type std::money_base::format describing the formatting used by this locale. That’s the whole vibe: locale-specific money-format structure, served fresh 🫢


πŸ“ Notes πŸ“

Okay so here’s the little plot twist: while std::money_put uses pos_format for formatting positive values and neg_format for formatting negative values, std::money_get uses neg_format for parsing all monetary values. It assumes that neg_format is compatible with pos_format.

Bestie, that compatibility assumption is lowkey important β€” std::money_get is out here using neg_format for everything, no cap πŸ’€

πŸ§ͺ 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) {}
            pattern do_pos_format() const { return {value, space, symbol,
       sign}; }
            pattern do_neg_format() const { return {value, space, symbol,
       sign}; }
        };

        int main()
        {
            std::cout.imbue(std::locale("en_US.utf8"));
            std::cout << "american locale: " << std::showbase
                      << std::put_money(12345678.0) << '\n';

            std::cout.imbue(std::locale(std::cout.getloc(), new
       my_punct("en_US.utf8")));
            std::cout << "locale with modified moneypunct:\n"
                      << std::put_money(12345678.0) << '\n'
                      << std::put_money(-12345678.0) << '\n';
        }

πŸ’¬ Output πŸ’¬

american locale: $123,456.78
locale with modified moneypunct:
123,456.78 $
123,456.78 $-

The way that modified moneypunct just flips the whole money vibe around? Obsessed 😭✨


πŸ‘€ See also πŸ‘€

  • do_curr_symbol β€” provides the string to use as the currency identifier. [virtual] (virtual protected member function)
  • do_positive_sign β€” provides the string to indicate a positive or negative value.
  • do_negative_sign β€” (virtual protected member function) and [virtual] β€” that’s literally how it’s listed, bestie 🫠
  • do_get β€” parses a monetary value from an input stream. [virtual] (virtual protected member function of std::money_get<CharT,InputIt>)
  • do_put β€” formats a monetary value and writes to output stream. [virtual] (virtual protected member function of std::money_put<CharT,OutputIt>)

Source/version sign-off, because we stan citations: http://cppreference.com, dated 2024.06.10 β€” mwah πŸ’‹πŸŒˆ

πŸ“¦ where this page comes from