名稱空間
變體
操作

std::put_money

來自 cppreference.com
< cpp‎ | io‎ | manip
 
 
 
輸入/輸出操縱器
浮點格式化
整數格式化
布林格式化
欄位寬度和填充控制
其他格式化
空白字元處理
輸出重新整理
(C++20)  

狀態標誌操作
時間與金錢 I/O
(C++11)
(C++11)
put_money
(C++11)
(C++11)
帶引號的操縱器
(C++14)
 
定義於標頭檔案 <iomanip>
template< class MoneyT >
/*unspecified*/ put_money( const MoneyT& mon, bool intl = false );
(C++11 起)

當在表示式 out << put_money(mon, intl) 中使用時,將貨幣值 mon 轉換為其字元表示形式,其方式由當前嵌入到 out 中的本地化區域的 std::money_put facet 指定。

out << put_money(mon, intl) 中的插入操作的行為類似於 FormattedOutputFunction

目錄

[編輯] 引數

mon - 貨幣值,可以是 long doublestd::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; // 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

[編輯] 參閱

將貨幣值格式化為字元序列輸出
(類模板) [編輯]
(C++11)
解析貨幣值
(函式模板) [編輯]