cplusplus.com
C++ : Reference : Miscellaneous : locale : numpunct : grouping
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
locale
has_facet
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
locale
tolower
toupper
use_facet
standard facets:
codecvt
codecvt_base
codecvt_byname
collate
collate_byname
ctype
ctype_base
ctype_byname
messages
messages_base
messages_byname
moneypunct
moneypunct_byname
money_base
money_get
money_put
numpunct
numpunct_byname
num_get
num_put
time_base
time_get
time_get_byname
time_put
time_put_byname
numpunct
numpunct::numpunct
public member functions:
numpunct::decimal_point
numpunct::falsename
numpunct::grouping
numpunct::thousands_sep
numpunct::truename
public member types:
numpunct::char_type
numpunct::string_type
protected members:
numpunct::do_decimal_point
numpunct::do_falsename
numpunct::do_grouping
numpunct::do_thousands_sep
numpunct::do_truename
numpunct::~numpunct


numpunct::grouping

public member function
string grouping() const;

Return grouping of digits

Returns a sequence of integer values indicating the number of digits in each group.

The sequence is returned as a string (basic_string<char>) where each char element shall be interpreted as an integer value indicating the number of digits in each group. Grouping is considered from right to left, with the first character in the string being used for the rightmost group, and the last character used for any group farther away than the length of the string.

For example, "\03" indicates groups of three digits each, while "\02\03" would indicate that the rightmost group has two digits and all the remaining ones have three digits each.

A negative value in the string or a value equal to CHAR_MAX indicates that the number of digits in the group is unlimited.

For the standard specializations of char and wchar_t the function returns an empty string, indicating no grouping.

During its operation, the version of this function in the generic template simply calls the virtual protected member do_grouping, which is the member function in charge of performing the actions described above.

Parameters

none

Return value

A string object containing a sequence of digit grouping values.
Notice that this is an object of type basic_string<char> no matter what character type is used as template parameter.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// numpunct::grouping example
#include <iostream>
#include <locale>
using namespace std;

string format_grouping (long value,string grouping)
{
  string result;
  long temp=value;
  size_t group=0;

  if (grouping.size()==0) grouping="\03";
  if (temp==0) return "0";

  while (true)
  {
    for (int i=0; i<grouping[group]; ++i)
	  {result=char('0'+temp%10)+result;temp/=10;if (temp==0) break;}
	if (grouping.size()>group+1) ++group;
	if (temp>0) result=","+result; else break;
  }
  return result;
}

int main ()
{
  string grouping = use_facet<numpunct<char> >(cout.getloc()).grouping();

  cout << format_grouping (43112279,grouping) << endl;

  return 0;
} 


Output:

43,112,279

See also