名前空間
変種
操作

std::numpunct<CharT>::grouping, std::numpunct<CharT>::do_grouping

From cppreference.com
< C++‎ | locale‎ | numpunct
 
 
 
 
 
ヘッダー <locale> で定義
public:
std::string grouping() const;
(1)
protected:
virtual std::string do_grouping() const;
(2)
1) 公開メンバ関数。最も派生したクラスのメンバ関数do_groupingを呼び出します。
2) num_put::put()(したがって、basic_ostream::operator<<)によってフォーマットされる数値出力の各グループの桁数を示す、std::stringを返します。

この関数は、整数値のベクトルとして使用される文字列vecを返します。(例: "\003" は各3桁のグループを指定し、"3" は各51桁のグループを示唆します)。各要素vec[i]は、数値の整数の部分のi番目の桁グループの桁数(右から数える)を表します。vec[0]は最も右のグループの桁数を保持し、vec[1]は右から2番目のグループの桁数を保持します。最後の文字vec[vec.size()-1]で示されるグループ化は、数値の(左部分の)残りのすべての桁をグループ化するために繰り返し再利用されます。 vec[i]が非正またはCHAR_MAXに等しい場合、対応する桁グループのサイズは無制限です。

[編集] 戻り値

グループを保持するstd::string型オブジェクト。std::numpunctの標準特殊化は空文字列を返し、グループ化がないことを示します。典型的なグループ化(例: `en_US`ロケール)は"\003"を返します。

[編集]

#include <iostream>
#include <limits>
#include <locale>
 
struct space_out : std::numpunct<char>
{
    char do_thousands_sep()   const { return ' ';  } // separate with spaces
    std::string do_grouping() const { return "\1"; } // groups of 1 digit
};
 
struct g123 : std::numpunct<char>
{
    std::string do_grouping() const { return "\1\2\3"; }
};
 
int main()
{
    std::cout << "Default locale: " << 12345678 << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new space_out));
    std::cout << "Locale with modified numpunct: " << 12345678 << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new g123));
    std::cout << "Locale with \\1\\2\\3 grouping: "
              << std::numeric_limits<unsigned long long>::max() << '\n'
              << "Same, for a floating-point number: "
              << std::fixed << 123456789.123456789 << '\n';
}

出力

Default locale: 12345678
Locale with modified numpunct: 1 2 3 4 5 6 7 8
Locale with \1\2\3 grouping: 18,446,744,073,709,551,61,5
Same, for a floating-point number: 123,456,78,9.123457

[編集] 関連項目

千単位の区切り文字として使用する文字を提供します
(virtual protected member function) [編集]
English 日本語 中文(简体) 中文(繁體)