namespace std {
template <class charT, bool International = false>
class moneypunct : public locale::facet, public money_base;
}
概要
moneypunctは、金額の入出力における書式(通貨記号、小数点、桁区切り、符号、出力順序)に関する情報を提供するロケールファセットである。
money_getとmoney_putは、このファセットから取得した情報を使って金額の解析・書式化を行う。
2つめのテンプレートパラメータInternationalがtrueである特殊化は、国際通貨表現(ISO 4217の3文字コード)の書式を提供する。
メンバ関数
publicメンバ関数
| 名前 | 説明 |
|---|---|
(constructor) |
コンストラクタ |
decimal_point |
小数点の文字を取得する |
thousands_sep |
桁区切りの文字を取得する |
grouping |
何桁で区切るかの、桁数のシーケンスを取得する |
curr_symbol |
通貨記号を取得する |
positive_sign |
正の金額を表す記号を取得する |
negative_sign |
負の金額を表す記号を取得する |
frac_digits |
金額の小数桁数 |
pos_format |
正の金額を出力するためのフォーマットを取得する |
neg_format |
負の金額を出力するためのフォーマットを取得する |
静的メンバ変数
| 名前 | 説明 |
|---|---|
static locale::id id; |
このファセットを識別するためのID |
static const bool intl = International; |
テンプレートパラメータInternationalの値 |
protectedメンバ関数
| 名前 | 説明 |
|---|---|
(destructor) |
デストラクタ |
do_decimal_point |
小数点の文字を取得する (virtual) |
do_thousands_sep |
桁区切りの文字を取得する (virtual) |
do_grouping |
何桁で区切るかの、桁数のシーケンスを取得する (virtual) |
do_curr_symbol |
通貨記号を取得する (virtual) |
do_positive_sign |
正の金額を表す記号を取得する (virtual) |
do_negative_sign |
負の金額を表す記号を取得する (virtual) |
do_frac_digits |
金額の小数桁数 (virtual) |
do_pos_format |
正の金額を出力するためのフォーマットを取得する (virtual) |
do_neg_format |
負の金額を出力するためのフォーマットを取得する (virtual) |
メンバ型
| 名前 | 説明 |
|---|---|
char_type |
文字型 charT |
string_type |
文字列型 std::basic_string<charT> |
例
基本的な使い方
#include <iostream>
#include <locale>
int main()
{
const auto& mp = std::use_facet<std::moneypunct<char>>(std::locale::classic());
std::cout << "decimal_point : [" << mp.decimal_point() << "]" << std::endl;
std::cout << "thousands_sep : [" << mp.thousands_sep() << "]" << std::endl;
std::cout << "curr_symbol : [" << mp.curr_symbol() << "]" << std::endl;
std::cout << "frac_digits : " << mp.frac_digits() << std::endl;
}
出力例
decimal_point : []
thousands_sep : []
curr_symbol : []
frac_digits : 0
"C"ロケールにおけるmoneypunctの各値は規格に規定がないため、出力は処理系によって異なる
ロケールによる違い
#include <iostream>
#include <locale>
#include <string>
#include <stdexcept>
std::string to_string(std::money_base::pattern p)
{
std::string result;
for (int i = 0; i < 4; ++i) {
if (i != 0) {
result += ' ';
}
switch (p.field[i]) {
case std::money_base::none: result += "none"; break;
case std::money_base::space: result += "space"; break;
case std::money_base::symbol: result += "symbol"; break;
case std::money_base::sign: result += "sign"; break;
case std::money_base::value: result += "value"; break;
}
}
return result;
}
void print_moneypunct(const char* name)
{
std::cout << name << std::endl;
try {
std::locale loc{name};
const auto& mp = std::use_facet<std::moneypunct<char>>(loc);
std::cout << " curr_symbol : [" << mp.curr_symbol() << "]" << std::endl;
std::cout << " decimal_point : [" << mp.decimal_point() << "]" << std::endl;
std::cout << " thousands_sep : [" << mp.thousands_sep() << "]" << std::endl;
std::cout << " frac_digits : " << mp.frac_digits() << std::endl;
std::cout << " pos_format : " << to_string(mp.pos_format()) << std::endl;
}
catch (const std::runtime_error&) {
// 指定した名前のロケールが利用できない場合
std::cout << " not available" << std::endl;
}
}
int main()
{
print_moneypunct("en_US.UTF-8");
print_moneypunct("ja_JP.UTF-8");
print_moneypunct("de_DE.UTF-8");
}
出力例
en_US.UTF-8
curr_symbol : [$]
decimal_point : [.]
thousands_sep : [,]
frac_digits : 2
pos_format : sign symbol none value
ja_JP.UTF-8
curr_symbol : [¥]
decimal_point : [.]
thousands_sep : [,]
frac_digits : 0
pos_format : sign symbol none value
de_DE.UTF-8
curr_symbol : [ €]
decimal_point : [,]
thousands_sep : [.]
frac_digits : 2
pos_format : sign value none symbol
- 日本円は補助単位を持たないため
frac_digits()が0であり、米ドルとユーロは2となる - ドイツのロケールでは小数点と桁区切りの文字が米国と逆になっている
pos_format()のパターンでは、米国と日本はvalueより前にsymbolがあるため通貨記号が値の前に置かれ、ドイツはvalueより後ろにあるため値の後ろに置かれる- ドイツの通貨記号
" €"のように、記号自体が空白を含むことがある - 妥当なロケール名は処理系定義である。指定した名前のロケールが利用できない場合、
std::localeのコンストラクタはstd::runtime_errorを送出し、上記の例ではnot availableが出力される
バージョン
言語
- C++98