Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |locale‎ |numpunct
       
       
       
      Localization library
       
       
      Defined in header<locale>
      public:
      std::string grouping()const;
      (1)
      protected:
      virtualstd::string do_grouping()const;
      (2)
      1) Public member function, calls the member functiondo_grouping of the most derived class.
      2) Returns anstd::string holding, in eachchar element, the number of digits in each group of the numeric output formatted bynum_put::put() (and, therefore,basic_ostream::operator<<).

      This function returns a string,vec, which is used as a vector of integer values. (For example,"\003" specifies groups of 3 digits each, while"3" implies groups of 51 digits each.). Each elementvec[i] represents the number of digits in theith digit group of the integer part of the number, counting from the right:vec[0] holds the number of digits in the rightmost group,vec[1] - in the second group from the right, etc. The grouping indicated by the last character,vec[vec.size()-1], is repeatedly reused to group all remaining digits in the (left part of) the number. Ifvec[i] is non-positive or equalsCHAR_MAX the size of the corresponding digit group is unlimited.

      [edit]Return value

      The object of typestd::string holding the groups. The standard specializations ofstd::numpunct return an empty string, indicating no grouping. Typical groupings (e.g. theen_US locale) return"\003".

      [edit]Example

      Run this code
      #include <iostream>#include <limits>#include <locale> struct space_out:std::numpunct<char>{char do_thousands_sep()const{return' ';}// separate with spacesstd::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<unsignedlonglong>::max()<<'\n'<<"Same, for a floating-point number: "<<std::fixed<<123456789.123456789<<'\n';}

      Output:

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

      [edit]See also

      provides the character to use as thousands separator
      (virtual protected member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/numpunct/grouping&oldid=160329"

      [8]ページ先頭

      ©2009-2025 Movatter.jp