Namespaces
Variants

std:: put_money

From cppreference.net
< cpp ‎ | io ‎ | manip
定义于头文件 <iomanip>
template < class MoneyT >
/*unspecified*/ put_money ( const MoneyT & mon, bool intl = false ) ;
(C++11 起)

当在表达式 out << put_money ( mon, intl ) 中使用时,将货币值 mon 转换为其字符表示形式,该形式由当前植入 out 的 locale 的 std::money_put facet 所指定。

插入操作 out << put_money ( mon, intl ) 的行为表现为一个 格式化输出函数

目录

参数

mon - 货币值,可以是 long double std::basic_string
intl - true 时使用国际化货币字符串,否则使用货币符号

返回值

一个未指定类型的对象,使得

其中函数 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);
}

示例

#include <iomanip>
#include <iostream>
int main()
{
    long double mon = 123.45; // 或 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

参见

将货币值格式化为字符序列输出
(类模板)
(C++11)
解析货币值
(函数模板)