std::moneyp...housands_sep(3)

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

πŸ’– NAME πŸ’–

Welcome to the C++ Standard Libary manual, bestie β€” this page is std::moneyp...housands_sep(3), and the star of the show is std::moneypunct::thousands_sep,do_thousands_sep. That's std::moneypunct::thousands_sep,do_thousands_sep β€” yes, the name is giving "???", but we move. 🫢


πŸ¦‹ Synopsis πŸ¦‹

Defined in header <locale>
public:                                     (1)
char_type thousands_sep() const;
protected:                                  (2)
virtual char_type do_thousands_sep() const;

Number (1) is the public member function, and it's lowkey just a messenger: it calls the member function do_thousands_sep of the most derived class. Slay delegation. πŸ’…

Number (2) is the one that returns the character to be used as the separator between digit groups when parsing or formatting the integral parts of monetary values. Money formatting said "I need a separator" and this function said "say less." πŸ’Έ


✨ Return value ✨

It returns the object of type char_type to use as the thousands separator. In common U.S. locales, that's ',' or L','. No cap. πŸ“


🌸 Example 🌸

// Run this code

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

struct space_out : std::moneypunct<char>
{
    pattern do_pos_format() const { return {value, none, none, none}; }
    int do_frac_digits() const { return 0; }
    char_type do_thousands_sep() const { return ' '; }
    string_type do_grouping() const { return "\002"; }
};

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

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

πŸ’» Output πŸ’»

american locale: $123,456.78
locale with modified moneypunct: 12 34 56 78

πŸ‘€ See also πŸ‘€

  • do_grouping provides the numbers of digits between each pair of thousands separators β€” [virtual] (virtual protected member function).

Big cute sign-off: source/version is cppreference.com and 2024.06.10, page std::moneyp...housands_sep(3) β€” bye bestie, mwah πŸ’‹πŸ‘‘πŸŒˆ

πŸ“¦ where this page comes from