std::put_money
From cppreference.com
| ヘッダ <iomanip>で定義 |
||
| template< class MoneyT > /*unspecified*/ put_money( const MoneyT& mon, bool intl = false ); |
(C++11以降) | |
out << put_money(mon, intl) という式で使用されると、現在 out に設定されているロケールの std::money_put ファセットによって指定された文字列表現に、貨幣値 mon を変換します。
out << put_money(mon, intl) の挿入操作は、FormattedOutputFunction として機能します。
目次 |
[edit] Parameters
| mon | - | long double または std::basic_string のいずれかの貨幣値 |
| intl | - | 真の場合、国際通貨記号を使用します。それ以外の場合は、通貨記号を使用します。 |
[edit] Return value
未指定の型のオブジェクト。その結果、
- out が std::basic_ostream<CharT, Traits> 型のオブジェクトである場合、式 out << put_money(mon, intl)
- は std::basic_ostream<CharT, Traits>& 型を持ち、
outの値を持ち、- f(out, mon, intl) を呼び出す FormattedOutputFunction として機能します。
ここで関数 f は次のように定義されます。
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); }
[edit] Example
このコードを実行
#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'; }
実行結果の例
en_US: $1.23 or USD 1.23 ru_RU: 1.23 руб or 1.23 RUB ja_JP: ¥123 or JPY 123
[edit] See also
| 通貨値を文字列シーケンスとして出力するために書式設定する (クラステンプレート) | |
| (C++11) |
通貨の値を構文解析する (関数テンプレート) |