最終更新日時:
が更新

履歴 編集

function
<locale>

std::numpunct::grouping

string grouping() const; // (1) C++98

概要

何桁で区切るかの、桁数のシーケンスを取得する。

戻り値

do_grouping()の呼び出し結果を返す。

#include <iostream>
#include <locale>
#include <string>
#include <stdexcept>

int main()
{
  for (const char* name : {"C", "en_US.UTF-8", "ja_JP.UTF-8", "de_DE.UTF-8"}) {
    try {
      std::locale loc{name};
      const auto& np = std::use_facet<std::numpunct<char>>(loc);

      std::string g = np.grouping();

      if (g.empty()) {
        std::cout << name << " : (no grouping)" << std::endl;
      }
      else {
        // 各要素は文字ではなく桁数を表す数値である
        std::cout << name << " : " << static_cast<int>(g[0]) << std::endl;
      }
    }
    catch (const std::runtime_error&) {
      // 指定した名前のロケールが利用できない場合
      std::cout << name << " : not available" << std::endl;
    }
  }
}

出力例

C : (no grouping)
en_US.UTF-8 : 3
ja_JP.UTF-8 : 3
de_DE.UTF-8 : 3

  • "C"ロケールでは桁区切りを行わないため、空文字列が返る
  • 戻り値の各要素は文字ではなく数値として解釈される。3桁ごとの区切りは'3'(文字)ではなく3(数値)である
  • 妥当なロケール名は処理系定義である。指定した名前のロケールが利用できない場合、std::localeのコンストラクタはstd::runtime_errorを送出し、上記の例ではnot availableが出力される

バージョン

言語

  • C++98

関連項目