namespace std {
template <class moneyT>
unspecified put_money(const moneyT& mon, bool intl = false); // (1) C++11
}
概要
金額書式で出力する。
intlにtrueを指定した場合は国際通貨表記 (JPYのような通貨記号) 、falseを指定した場合は各国内での通貨表記 (¥のような通貨記号) として出力する。
要件
型moneyTは、long doubleもしくはbasic_stringの特殊化のいずれかであること。
戻り値
未規定の型のオブジェクトを返す。outをbasic_ostream<CharT, Traits>型のオブジェクトとして、式out << put_money(mon, intl)は以下の関数fをf(out, mon, intl)と呼び出したかのように振る舞う:
template <class CharT, class Traits, class moneyT>
void f(basic_ios<CharT, Traits>& str, const moneyT& mon, bool intl)
{
using Iter = ostreambuf_iterator<CharT, Traits>;
using MoneyPut = money_put<CharT, Iter>;
const MoneyPut& mp = use_facet<MoneyPut>(str.getloc());
const Iter end = mp.put(Iter(str.rdbuf()), intl, str, str.fill(), mon);
if (end.failed())
str.setstate(ios_base::badbit);
}
式out << put_money(mon, intl)は、basic_ostream<CharT, Traits>&型を持ち、その値はoutである。
備考
出力する金額は、通貨の最小単位 (日本円なら円、米ドルならセント) を単位とする値として渡す。小数点の位置や通貨記号の有無は、ストリームに設定されているロケールのmoneypunctファセットによって決まる。
例
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
// Cロケールでは通貨記号がなく、小数点以下の桁数は0となる
// long doubleで指定する
std::cout << std::put_money(123456.0L) << std::endl;
// 文字列で指定する
std::cout << std::put_money(std::string("987")) << std::endl;
// 国際通貨表記で出力する
std::cout << std::put_money(123456.0L, true) << std::endl;
}
出力
123456
987
123456
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 5.1 ✅
- ICC: ??
- Visual C++: ??