std::put_money
來自 cppreference.com
定義於標頭檔案 <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 double 或 std::basic_string |
intl | - | 如果 true,則使用國際貨幣字串,否則使用貨幣符號 |
[編輯] 返回值
一個未指定型別的物件,使得
- 如果 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); }
[編輯] 示例
執行此程式碼
#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) |
解析貨幣值 (函式模板) |