|
ICU 61.1
61.1
|
C++ API: Library for localized number formatting introduced in ICU 60. More...
#include "unicode/utypes.h"#include "unicode/appendable.h"#include "unicode/dcfmtsym.h"#include "unicode/currunit.h"#include "unicode/fieldpos.h"#include "unicode/fpositer.h"#include "unicode/measunit.h"#include "unicode/nounit.h"#include "unicode/plurrule.h"#include "unicode/ucurr.h"#include "unicode/unum.h"Go to the source code of this file.
Data Structures | |
| class | icu::number::Notation |
| A class that defines the notation style to be used when formatting numbers in NumberFormatter. More... | |
| struct | icu::number::Notation::NotationUnion::ScientificSettings |
| class | icu::number::ScientificNotation |
| A class that defines the scientific notation style to be used when formatting numbers in NumberFormatter. More... | |
| class | icu::number::Rounder |
| A class that defines the rounding strategy to be used when formatting numbers in NumberFormatter. More... | |
| struct | icu::number::Rounder::RounderUnion::FractionSignificantSettings |
| struct | icu::number::Rounder::RounderUnion::IncrementSettings |
| class | icu::number::FractionRounder |
| A class that defines a rounding strategy based on a number of fraction places and optionally significant digits to be used when formatting numbers in NumberFormatter. More... | |
| class | icu::number::CurrencyRounder |
| A class that defines a rounding strategy parameterized by a currency to be used when formatting numbers in NumberFormatter. More... | |
| class | icu::number::IncrementRounder |
| A class that defines a rounding strategy parameterized by a rounding increment to be used when formatting numbers in NumberFormatter. More... | |
| class | icu::number::IntegerWidth |
| A class that defines the strategy for padding and truncating integers before the decimal separator. More... | |
| class | icu::number::impl::SymbolsWrapper |
| class | icu::number::impl::Grouper |
| class | icu::number::impl::Padder |
| struct | icu::number::impl::MacroProps |
| class | icu::number::NumberFormatterSettings< Derived > |
| An abstract base class for specifying settings related to number formatting. More... | |
| class | icu::number::UnlocalizedNumberFormatter |
| A NumberFormatter that does not yet have a locale. More... | |
| class | icu::number::LocalizedNumberFormatter |
| A NumberFormatter that has a locale associated with it; this means .format() methods are available. More... | |
| class | icu::number::FormattedNumber |
| The result of a number formatting operation. More... | |
| class | icu::number::NumberFormatter |
| See the main description in numberformatter.h for documentation and examples. More... | |
Namespaces | |
| icu | |
| File coll.h. | |
Typedefs | |
| typedef enum UNumberUnitWidth | UNumberUnitWidth |
| An enum declaring how to render units, including currencies. More... | |
| typedef enum UGroupingStrategy | UGroupingStrategy |
| An enum declaring the strategy for when and how to display grouping separators (i.e., the separator, often a comma or period, after every 2-3 powers of ten). More... | |
| typedef enum UNumberSignDisplay | UNumberSignDisplay |
| An enum declaring how to denote positive and negative numbers. More... | |
| typedef enum UNumberDecimalSeparatorDisplay | UNumberDecimalMarkDisplay |
| An enum declaring how to render the decimal separator. More... | |
| typedef int16_t | icu::number::impl::digits_t |
| Datatype for minimum/maximum fraction digits. More... | |
| typedef Notation | icu::number::CompactNotation |
| typedef Notation | icu::number::SimpleNotation |
| typedef Rounder | icu::number::DigitRounder |
C++ API: Library for localized number formatting introduced in ICU 60.
This library was introduced in ICU 60 to simplify the process of formatting localized number strings. Basic usage examples:
// Most basic usage: NumberFormatter::withLocale(...).format(123).toString(); // 1,234 in en-US
// Custom notation, unit, and rounding strategy:
NumberFormatter::with()
.notation(Notation::compactShort())
.unit(CurrencyUnit("EUR", status))
.rounding(Rounder::maxDigits(2))
.locale(...)
.format(1234)
.toString(); // €1.2K in en-US// Create a formatter in a singleton for use later:
static const LocalizedNumberFormatter formatter = NumberFormatter::withLocale(...)
.unit(NoUnit::percent())
.rounding(Rounder::fixedFraction(3));
formatter.format(5.9831).toString(); // 5.983% in en-US// Create a "template" in a singleton but without setting a locale until the call site:
static const UnlocalizedNumberFormatter template = NumberFormatter::with()
.sign(UNumberSignDisplay::UNUM_SIGN_ALWAYS)
.adoptUnit(MeasureUnit::createMeter(status))
.unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME);
template.locale(...).format(1234).toString(); // +1,234 meters in en-US
This API offers more features than DecimalFormat and is geared toward new users of ICU.
NumberFormatter instances are immutable and thread safe. This means that invoking a configuration method has no effect on the receiving instance; you must store and use the new number formatter instance it returns instead.
UnlocalizedNumberFormatter formatter = UnlocalizedNumberFormatter::with().notation(Notation::scientific()); formatter.rounding(Rounder.maxFraction(2)); // does nothing! formatter.locale(Locale.getEnglish()).format(9.8765).toString(); // prints "9.8765E0", not "9.88E0"
This API is based on the fluent design pattern popularized by libraries such as Google's Guava. For extensive details on the design of this API, read the design doc.
Definition in file numberformatter.h.
| typedef int16_t icu::number::impl::digits_t |
Datatype for minimum/maximum fraction digits.
Must be able to hold kMaxIntFracSig.
Definition at line 413 of file numberformatter.h.
| typedef enum UGroupingStrategy UGroupingStrategy |
An enum declaring the strategy for when and how to display grouping separators (i.e., the separator, often a comma or period, after every 2-3 powers of ten).
The choices are several pre-built strategies for different use cases that employ locale data whenever possible. Example outputs for 1234 and 1234567 in en-IN:
The default is AUTO, which displays grouping separators unless the locale data says that grouping is not customary. To force grouping for all numbers greater than 1000 consistently across locales, use ON_ALIGNED. On the other hand, to display grouping less frequently than the default, use MIN2 or OFF. See the docs of each option for details.
Note: This enum specifies the strategy for grouping sizes. To set which character to use as the grouping separator, use the "symbols" setter.
| typedef enum UNumberDecimalSeparatorDisplay UNumberDecimalMarkDisplay |
An enum declaring how to render the decimal separator.
| typedef enum UNumberSignDisplay UNumberSignDisplay |
An enum declaring how to denote positive and negative numbers.
Example outputs when formatting 123, 0, and -123 in en-US:
The exact format, including the position and the code point of the sign, differ by locale.
| typedef enum UNumberUnitWidth UNumberUnitWidth |
An enum declaring how to render units, including currencies.
Example outputs when formatting 123 USD and 123 meters in en-CA:
This enum is similar to com.ibm.icu.text.MeasureFormat.FormatWidth.
| enum UGroupingStrategy |
An enum declaring the strategy for when and how to display grouping separators (i.e., the separator, often a comma or period, after every 2-3 powers of ten).
The choices are several pre-built strategies for different use cases that employ locale data whenever possible. Example outputs for 1234 and 1234567 in en-IN:
The default is AUTO, which displays grouping separators unless the locale data says that grouping is not customary. To force grouping for all numbers greater than 1000 consistently across locales, use ON_ALIGNED. On the other hand, to display grouping less frequently than the default, use MIN2 or OFF. See the docs of each option for details.
Note: This enum specifies the strategy for grouping sizes. To set which character to use as the grouping separator, use the "symbols" setter.
| Enumerator | |
|---|---|
| UNUM_GROUPING_OFF |
Do not display grouping separators in any locale.
|
| UNUM_GROUPING_MIN2 |
Display grouping using locale defaults, except do not show grouping on values smaller than 10000 (such that there is a minimum of two digits before the first separator). Note that locales may restrict grouping separators to be displayed only on 1 million or greater (for example, ee and hu) or disable grouping altogether (for example, bg currency). Locale data is used to determine whether to separate larger numbers into groups of 2 (customary in South Asia) or groups of 3 (customary in Europe and the Americas).
|
| UNUM_GROUPING_AUTO |
Display grouping using the default strategy for all locales. This is the default behavior. Note that locales may restrict grouping separators to be displayed only on 1 million or greater (for example, ee and hu) or disable grouping altogether (for example, bg currency). Locale data is used to determine whether to separate larger numbers into groups of 2 (customary in South Asia) or groups of 3 (customary in Europe and the Americas).
|
| UNUM_GROUPING_ON_ALIGNED |
Always display the grouping separator on values of at least 1000. This option ignores the locale data that restricts or disables grouping, described in MIN2 and AUTO. This option may be useful to normalize the alignment of numbers, such as in a spreadsheet. Locale data is used to determine whether to separate larger numbers into groups of 2 (customary in South Asia) or groups of 3 (customary in Europe and the Americas).
|
| UNUM_GROUPING_THOUSANDS |
Use the Western defaults: groups of 3 and enabled for all numbers 1000 or greater. Do not use locale data for determining the grouping strategy.
|
Definition at line 193 of file numberformatter.h.
An enum declaring how to render the decimal separator.
| Enumerator | |
|---|---|
| UNUM_DECIMAL_SEPARATOR_AUTO |
Show the decimal separator when there are one or more digits to display after the separator, and do not show it otherwise. This is the default behavior.
|
| UNUM_DECIMAL_SEPARATOR_ALWAYS |
Always show the decimal separator, even if there are no digits to display after the separator.
|
| UNUM_DECIMAL_SEPARATOR_COUNT |
One more than the highest UNumberDecimalSeparatorDisplay value.
|
Definition at line 364 of file numberformatter.h.
| enum UNumberSignDisplay |
An enum declaring how to denote positive and negative numbers.
Example outputs when formatting 123, 0, and -123 in en-US:
The exact format, including the position and the code point of the sign, differ by locale.
| Enumerator | |
|---|---|
| UNUM_SIGN_AUTO |
Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default behavior.
|
| UNUM_SIGN_ALWAYS |
Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. To hide the sign on zero, see UNUM_SIGN_EXCEPT_ZERO.
|
| UNUM_SIGN_NEVER |
Do not show the sign on positive or negative numbers.
|
| UNUM_SIGN_ACCOUNTING |
Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair of parentheses around the number. Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the future.
|
| UNUM_SIGN_ACCOUNTING_ALWAYS |
Use the locale-dependent accounting format on negative numbers, and show the plus sign on positive numbers, including zero. For more information on the accounting format, see the ACCOUNTING sign display strategy. To hide the sign on zero, see UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO.
|
| UNUM_SIGN_EXCEPT_ZERO |
Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a sign on zero.
|
| UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO |
Use the locale-dependent accounting format on negative numbers, and show the plus sign on positive numbers. Do not show a sign on zero. For more information on the accounting format, see the ACCOUNTING sign display strategy.
|
| UNUM_SIGN_COUNT |
One more than the highest UNumberSignDisplay value.
|
Definition at line 277 of file numberformatter.h.
| enum UNumberUnitWidth |
An enum declaring how to render units, including currencies.
Example outputs when formatting 123 USD and 123 meters in en-CA:
This enum is similar to com.ibm.icu.text.MeasureFormat.FormatWidth.
| Enumerator | |
|---|---|
| UNUM_UNIT_WIDTH_NARROW |
Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more information on the difference between NARROW and SHORT, see SHORT. In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for currencies.
|
| UNUM_UNIT_WIDTH_SHORT |
Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or symbol when there may be ambiguity. This is the default behavior. For example, in es-US, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°", since Fahrenheit is the customary unit for temperature in that locale. In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for currencies.
|
| UNUM_UNIT_WIDTH_FULL_NAME |
Print the full name of the unit, without any abbreviations. In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for currencies.
|
| UNUM_UNIT_WIDTH_ISO_CODE |
Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this option is currently undefined for use with measure units. In CLDR, this option corresponds to the "¤¤" placeholder for currencies.
|
| UNUM_UNIT_WIDTH_HIDDEN |
Format the number according to the specified unit, but do not display the unit. For currencies, apply monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is equivalent to not specifying the unit at all.
|
| UNUM_UNIT_WIDTH_COUNT |
One more than the highest UNumberUnitWidth value.
|
Definition at line 95 of file numberformatter.h.
1.8.11