Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/numpunct"

From cppreference.com
< cpp‎ | locale
(expand description)
m (fmt)
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{cpp/title | numpunct}}
+
{{cpp/title|numpunct}}
 
{{cpp/locale/numpunct/navbar}}
 
{{cpp/locale/numpunct/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | locale}}
+
{{dcl header|locale}}
{{ddcl list item | 1=
+
{{dcl|1=
 
template< class CharT >
 
template< class CharT >
 
class numpunct;
 
class numpunct;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
The facet {{c|std::numpunct}} encapsulates numeric punctuation preferences. Stream I/O operations use {{c|std::numpunct}} through {{c|std::num_get}} and {{c|std::num_put}} for parsing numeric input and formatting numeric output.  
+
The facet {{tt|std::numpunct}} encapsulates numeric punctuation preferences. Stream I/O operations use {{tt|std::numpunct}} through {{lc|std::num_get}} and {{lc|std::num_put}} for parsing numeric input and formatting numeric output.  
  
The numbers that are supported by {{c|std::numpunct}} have the format described below. Here {{tt|digit}} represents the radix set specified by the {{tt|fmtflags}} argument value, {{tt|thousands-sep}} and {{tt|decimal-point}} are the results of {{rlf|thousands_sep}} and {{rlf|decimal_point}} functions respectively. The format of integer values is as follows:
+
The numbers that are supported by {{tt|std::numpunct}} have the format described below. Here {{tt|digit}} represents the radix set specified by the {{tt|fmtflags}} argument value, {{tt|thousands-sep}} and {{tt|decimal-point}} are the results of {{lc|thousands_sep()}} and {{lc|decimal_point()}} functions respectively. The format of integer values is as follows:
  
 
{{source|lang=text|1=
 
{{source|lang=text|1=
Line 21: Line 21:
 
}}
 
}}
  
The number of digits between the {{tt|thousand-sep}}s (maximum size of {{tt|digits}}) is specified by the result of {{rlf|grouping}}.
+
The number of digits between the {{tt|thousand-sep}}s (maximum size of {{tt|digits}}) is specified by the result of {{lc|grouping()}}.
  
 
The format of floating-point values is as follows:
 
The format of floating-point values is as follows:
Line 27: Line 27:
 
floatval    ::= [sign] units [decimal-point [digits]] [e [sign] digits] {{!}}
 
floatval    ::= [sign] units [decimal-point [digits]] [e [sign] digits] {{!}}
 
                 [sign]        decimal-point  digits  [e [sign] digits]
 
                 [sign]        decimal-point  digits  [e [sign] digits]
e          ::= ’e’ | ’E’
+
e          ::= 'e' {{!}} 'E'
 
}}
 
}}
  
 
{{inheritance diagram/std-numpunct}}
 
{{inheritance diagram/std-numpunct}}
  
Two specializations are provided by the standard library
+
===Specializations===
 
+
The standard library is guaranteed to provide the following specializations (they are {{rlp|locale|required to be implemented by any locale object}}):
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | locale }}
+
{{dsc header|locale}}
{{dcl list item | {{c|std::numpunct<char>}} | provides equivalents of the "C" locale preferences }}
+
{{dsc|{{c/core|std::numpunct<char>}}|provides equivalents of the "C" locale preferences}}
{{dcl list item | {{c|std::numpunct<wchar_t>}} | provides wide character equivalents of the "C" locale preferences }}
+
{{dsc|{{c/core|std::numpunct<wchar_t>}}|provides wide character equivalents of the "C" locale preferences}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Member types===
 
===Member types===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dcl list item | {{tt|char_type}} | {{tt|charT}}}}
+
{{dsc|{{tt|char_type}}|{{tt|CharT}}}}
{{dcl list item | {{tt|string_type}} | {{c|std::basic_string<charT>}}}}
+
{{dsc|{{tt|string_type}}|{{c/core|std::basic_string<CharT>}}}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/locale/numpunct/dcl list numpunct}}
+
{{dsc inc|cpp/locale/numpunct/dsc numpunct}}
{{dcl list template | cpp/locale/numpunct/dcl list ~numpunct}}
+
{{dsc inc|cpp/locale/numpunct/dsc ~numpunct}}
{{dcl list h2 | }}
+
{{dsc h2|}}
{{dcl list template | cpp/locale/numpunct/dcl list decimal_point}}
+
{{dsc inc|cpp/locale/numpunct/dsc decimal_point}}
{{dcl list template | cpp/locale/numpunct/dcl list thousands_sep}}
+
{{dsc inc|cpp/locale/numpunct/dsc thousands_sep}}
{{dcl list template | cpp/locale/numpunct/dcl list grouping}}
+
{{dsc inc|cpp/locale/numpunct/dsc grouping}}
{{dcl list template | cpp/locale/numpunct/dcl list truename}}
+
{{dsc inc|cpp/locale/numpunct/dsc truefalsename}}
{{dcl list template | cpp/locale/numpunct/dcl list falsename}}
+
{{dsc end}}
{{dcl list end}}
+
  
 
===Protected member functions===
 
===Protected member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/locale/numpunct/dcl list do_decimal_point}}
+
{{dsc inc|cpp/locale/numpunct/dsc do_decimal_point}}
{{dcl list template | cpp/locale/numpunct/dcl list do_thousands_sep}}
+
{{dsc inc|cpp/locale/numpunct/dsc do_thousands_sep}}
{{dcl list template | cpp/locale/numpunct/dcl list do_grouping}}
+
{{dsc inc|cpp/locale/numpunct/dsc do_grouping}}
{{dcl list template | cpp/locale/numpunct/dcl list do_truename}}
+
{{dsc inc|cpp/locale/numpunct/dsc do_truefalsename}}
{{dcl list template | cpp/locale/numpunct/dcl list do_falsename}}
+
{{dsc end}}
{{dcl list end}}
+
  
 
===Member objects===
 
===Member objects===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem obj | nolink=true | {{dcl small|static std::locale::id}} id | ''id'' of the locale }}
+
{{dsc mem obj|nolink=true|{{dsc small|static std::locale::id}} id|''id'' of the locale}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Example===
 
===Example===
 
 
{{example
 
{{example
| The following example changes the string representations of {{c|true}} and {{c|false}}
+
|The following example changes the string representations of {{c|true}} and {{c|false}}:
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <locale>
 
#include <locale>
  
struct french_bool : std::numpunct<char> {
+
struct french_bool : std::numpunct<char>
     string_type do_truename() const { return "oui"; }
+
{
     string_type do_falsename() const { return "non"; }
+
     string_type do_truename() const override { return "vrai"; }
 +
     string_type do_falsename() const override { return "faux"; }
 
};
 
};
  
Line 94: Line 92:
 
               << std::boolalpha << true << ", " << false << '\n';
 
               << std::boolalpha << true << ", " << false << '\n';
 
}
 
}
| output=
+
|output=
 
default locale: true, false
 
default locale: true, false
locale with modified numpunct: oui, non
+
locale with modified numpunct: vrai, faux
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=338|std=C++98|before=the {{tt|sign}} token allowed an optional whitespace following {{tt|+}} or {{tt|-}}|after=removed the whitespace}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list tclass | cpp/locale/numpunct_byname | creates a numpunct facet for the named locale }}
+
{{dsc tclass|cpp/locale/numpunct_byname|creates a numpunct facet for the named locale}}
{{dcl list end}}
+
{{dsc end}}
  
[[de:cpp/locale/numpunct]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/locale/numpunct]]
+
[[fr:cpp/locale/numpunct]]
+
[[it:cpp/locale/numpunct]]
+
[[ja:cpp/locale/numpunct]]
+
[[pt:cpp/locale/numpunct]]
+
[[ru:cpp/locale/numpunct]]
+
[[zh:cpp/locale/numpunct]]
+

Latest revision as of 02:53, 13 December 2023

 
 
 
 
Defined in header <locale>
template< class CharT >
class numpunct;

The facet std::numpunct encapsulates numeric punctuation preferences. Stream I/O operations use std::numpunct through std::num_get and std::num_put for parsing numeric input and formatting numeric output.

The numbers that are supported by std::numpunct have the format described below. Here digit represents the radix set specified by the fmtflags argument value, thousands-sep and decimal-point are the results of thousands_sep() and decimal_point() functions respectively. The format of integer values is as follows:

integer     ::= [sign] units
sign        ::= plusminus
plusminus   ::= '+' | '-'
units       ::= digits [thousands-sep units]
digits      ::= digit [digits]

The number of digits between the thousand-seps (maximum size of digits) is specified by the result of grouping().

The format of floating-point values is as follows:

floatval    ::= [sign] units [decimal-point [digits]] [e [sign] digits] |
                [sign]        decimal-point  digits   [e [sign] digits]
e           ::= 'e' | 'E'
cpp/locale/locale/facetstd-numpunct-inheritance.svg

Inheritance diagram

Contents

[edit] Specializations

The standard library is guaranteed to provide the following specializations (they are required to be implemented by any locale object):

Defined in header <locale>
std::numpunct<char> provides equivalents of the "C" locale preferences
std::numpunct<wchar_t> provides wide character equivalents of the "C" locale preferences

[edit] Member types

Member type Definition
char_type CharT
string_type std::basic_string<CharT>

[edit] Member functions

constructs a new numpunct facet
(public member function) [edit]
destructs a numpunct facet
(protected member function) [edit]
invokes do_decimal_point
(public member function) [edit]
invokes do_thousands_sep
(public member function) [edit]
invokes do_grouping
(public member function) [edit]
invokes do_truename or do_falsename
(public member function) [edit]

[edit] Protected member functions

provides the character to use as decimal point
(virtual protected member function) [edit]
provides the character to use as thousands separator
(virtual protected member function) [edit]
[virtual]
provides the numbers of digits between each pair of thousands separators
(virtual protected member function) [edit]
provides the string to use as the name of the boolean true and false
(virtual protected member function) [edit]

[edit] Member objects

static std::locale::id id
id of the locale
(public member object)

[edit] Example

The following example changes the string representations of true and false:

#include <iostream>
#include <locale>
 
struct french_bool : std::numpunct<char>
{
    string_type do_truename() const override { return "vrai"; }
    string_type do_falsename() const override { return "faux"; }
};
 
int main()
{
    std::cout << "default locale: "
              << std::boolalpha << true << ", " << false << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new french_bool));
    std::cout << "locale with modified numpunct: "
              << std::boolalpha << true << ", " << false << '\n';
}

Output:

default locale: true, false
locale with modified numpunct: vrai, faux

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 338 C++98 the sign token allowed an optional whitespace following + or - removed the whitespace

[edit] See also

creates a numpunct facet for the named locale
(class template)