std::get_money(3)

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

πŸ’Έ std::get_money(3) πŸ’Έ

okay so bestie, this page is from the C++ Standard Libary manual (yes, Libary, that's literally the header's spelling 😭 no cap) and it's all about the iconic std::get_money.


πŸŽ€ NAME πŸŽ€

std::get_money β€” std::get_money (the name ate and left no crumbs πŸ’…)

✨ Synopsis ✨

bestie it's Defined in header <iomanip>, and the signature is this:

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

the template< class MoneyT > line is since C++11 🫢

When you use it in an expression like in >> get_money(mon, intl), it parses the character input as a monetary value, as specified by the std::money_get facet of the locale currently imbued in in, and stores the value in mon.

The extraction operation in in >> get_money(mon, intl) behaves as a FormattedInputFunction β€” periodt.

🌸 Parameters 🌸

  • mon β€” variable where the monetary value will be written. Can be either long double or std::basic_string. it's giving flexible πŸ’…
  • intl β€” if true, it expects to find required international currency strings; otherwise it expects optional currency symbols.

πŸ‘‘ Return value πŸ‘‘

You get back an object of unspecified type such that:

  • if in is an object of type std::basic_istream<CharT, Traits>, the expression in >> get_money(mon, intl)
    • has type std::basic_istream<CharT, Traits>&
    • has value in
    • behaves as if it called f(in, mon, intl)

where the function f is defined as:

template<class CharT, class Traits, class MoneyT>
void f(std::basic_ios<CharT, Traits>& str, MoneyT& mon, bool intl)
{
    using Iter = std::istreambuf_iterator<CharT, Traits>;
    using MoneyGet = std::money_get<CharT, Iter>;

    std::ios_base::iostate err = std::ios_base::goodbit;
    const MoneyGet& mg = std::use_facet<MoneyGet>(str.getloc());

    mg.get(Iter(str.rdbuf()), Iter(), intl, str, err, mon);

    if (err != std::ios_base::goodbit)
        str.setstate(err);
}

🧚 Example 🧚

the original literally says // Run this code and then:

#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>

int main()
{
    std::istringstream in("$1,234.56 2.22 USD  3.33");
    long double v1, v2;
    std::string v3;

    in.imbue(std::locale("en_US.UTF-8"));
    in >> std::get_money(v1) >> std::get_money(v2) >>
       std::get_money(v3, true);

    if (in)
        std::cout << std::quoted(in.str()) << " parsed as: "
                  << v1 << ", " << v2 << ", " << v3 << '\n';
    else
        std::cout << "Parse failed";
}

πŸ’– Output πŸ’–

and the output is giving:

"$1,234.56 2.22 USD  3.33" parsed as: 123456, 222, 333

πŸ¦‹ See also πŸ¦‹

  • money_get β€” parses and constructs a monetary value from an input character sequence (class template)
  • put_money β€” formats and outputs a monetary value (C++11) (function template)

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::get_money(3) β€” bye bestie, slay your money parsing πŸ’…βœ¨

πŸ“¦ where this page comes from