std::put_money(3)

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

🌸 NAME 🌸

std::put_money β€” std::put_money. Bestie, the NAME section is literally repeating itself and honestly? Iconic behavior. This is the std::put_money(3) page from the C++ Standard Libary (that spelling is from the header, not me πŸ’…).


✨ Synopsis ✨

Defined in header <iomanip> β€” yes, that's where it lives, bestie.

Since C++11 (the template was born then, periodt):

template< class MoneyT >
/*unspecified*/ put_money( const MoneyT& mon, bool intl = false );

When used in an expression out << put_money(mon, intl), it converts the monetary value mon to its character representation as specified by the std::money_put facet of the locale currently imbued in out.

The insertion operation in out << put_money(mon, intl) behaves as a FormattedOutputFunction. No cap, that's the whole vibe check.


πŸŽ€ Parameters πŸŽ€

  • mon β€” a monetary value, either long double or std::basic_string.
  • intl β€” use international currency strings if true, use currency symbols otherwise.

πŸ’– Return value πŸ’–

An object of unspecified type such that:

  • if out is an object of type std::basic_ostream<CharT, Traits>, the expression out << put_money(mon, intl)
    • has type std::basic_ostream<CharT, Traits>&
    • has value out
    • behaves as a FormattedOutputFunction that calls f(out, mon, intl)

where the function f is defined as:

template<class CharT, class Traits, class MoneyT>
void f(std::basic_ios<CharT, Traits>& str, const MoneyT& mon, bool intl)
{
    using Iter = std::ostreambuf_iterator<CharT, Traits>;
    using MoneyPut = std::money_put<CharT, Iter>;

    const MoneyPut& mp = std::use_facet<MoneyPut>(str.getloc());
    const Iter end = mp.put(Iter(str.rdbuf()), intl, str, str.fill(), mon);

    if (end.failed())
        str.setstate(std::ios_base::badbit);
}

🧚 Example 🧚

The page literally says // Run this code, so here's the code, bestie:

#include <iomanip>
#include <iostream>

int main()
{
    long double mon = 123.45; // or std::string mon = "123.45";

    std::cout.imbue(std::locale("en_US.UTF-8"));
    std::cout << std::showbase
              << "en_US: " << std::put_money(mon)
              << " or " << std::put_money(mon, true) << '\n';

    std::cout.imbue(std::locale("ru_RU.UTF-8"));
    std::cout << "ru_RU: " << std::put_money(mon)
              << " or " << std::put_money(mon, true) << '\n';

    std::cout.imbue(std::locale("ja_JP.UTF-8"));
    std::cout << "ja_JP: " << std::put_money(mon)
              << " or " << std::put_money(mon, true) << '\n';
}

πŸ“ Possible output πŸ“

en_US: $1.23 or USD  1.23
ru_RU: 1.23 Ρ€ΡƒΠ± or 1.23 RUB
ja_JP: οΏ₯123 or JPY  123

πŸ¦‹ See also πŸ¦‹

  • money_put β€” formats a monetary value for output as a character sequence (class template)
  • get_money β€” parses a monetary value (C++11) (function template)

okay bestie, that's the whole page πŸ«ΆπŸ’– β€” source: cppreference.com, version/date: 2024.06.10. now go format some money and slay βœ¨πŸ‘‘

πŸ“¦ where this page comes from