ICU 60.1  60.1
Data Structures | Namespaces | Typedefs | Enumerations
numberformatter.h File Reference

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::Grouper
 
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::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 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 Notation icu::number::CompactNotation
 
typedef Notation icu::number::SimpleNotation
 
typedef Rounder icu::number::DigitRounder
 

Enumerations

enum  UNumberUnitWidth {
  UNUM_UNIT_WIDTH_NARROW, UNUM_UNIT_WIDTH_SHORT, UNUM_UNIT_WIDTH_FULL_NAME, UNUM_UNIT_WIDTH_ISO_CODE,
  UNUM_UNIT_WIDTH_HIDDEN, UNUM_UNIT_WIDTH_COUNT
}
 An enum declaring how to render units, including currencies. More...
 
enum  UNumberSignDisplay {
  UNUM_SIGN_AUTO, UNUM_SIGN_ALWAYS, UNUM_SIGN_NEVER, UNUM_SIGN_ACCOUNTING,
  UNUM_SIGN_ACCOUNTING_ALWAYS, UNUM_SIGN_COUNT
}
 An enum declaring how to denote positive and negative numbers. More...
 
enum  UNumberDecimalSeparatorDisplay { UNUM_DECIMAL_SEPARATOR_AUTO, UNUM_DECIMAL_SEPARATOR_ALWAYS, UNUM_DECIMAL_SEPARATOR_COUNT }
 An enum declaring how to render the decimal separator. More...
 

Detailed Description

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.

Author
Shane Carr

Definition in file numberformatter.h.

Typedef Documentation

◆ UNumberDecimalMarkDisplay

An enum declaring how to render the decimal separator.

  • UNUM_DECIMAL_SEPARATOR_AUTO: "1", "1.1"
  • UNUM_DECIMAL_SEPARATOR_ALWAYS: "1.", "1.1"

◆ UNumberSignDisplay

An enum declaring how to denote positive and negative numbers.

Example outputs when formatting 123 and -123 in en-US:

  • AUTO: "123", "-123"
  • ALWAYS: "+123", "-123"
  • NEVER: "123", "123"
  • ACCOUNTING: "$123", "($123)"
  • ACCOUNTING_ALWAYS: "+$123", "($123)"

The exact format, including the position and the code point of the sign, differ by locale.

Draft:
This API may be changed in the future versions and was introduced in ICU 60

◆ UNumberUnitWidth

An enum declaring how to render units, including currencies.

Example outputs when formatting 123 USD and 123 meters in en-CA:

  • NARROW*: "$123.00" and "123 m"
  • SHORT: "US$ 123.00" and "123 m"
  • FULL_NAME: "123.00 US dollars" and "123 meters"
  • ISO_CODE: "USD 123.00" and undefined behavior
  • HIDDEN: "123.00" and "123"
  • The narrow format for currencies is not currently supported; this is a known issue that will be fixed in a future version. See #11666 for more information.

This enum is similar to com.ibm.icu.text.MeasureFormat.FormatWidth.

Draft:
This API may be changed in the future versions and was introduced in ICU 60

Enumeration Type Documentation

◆ UNumberDecimalSeparatorDisplay

An enum declaring how to render the decimal separator.

  • UNUM_DECIMAL_SEPARATOR_AUTO: "1", "1.1"
  • UNUM_DECIMAL_SEPARATOR_ALWAYS: "1.", "1.1"
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_DECIMAL_SEPARATOR_ALWAYS 

Always show the decimal separator, even if there are no digits to display after the separator.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_DECIMAL_SEPARATOR_COUNT 

One more than the highest UNumberDecimalSeparatorDisplay value.

Internal:
Do not use. This API is for internal use only. ICU 60: The numeric value may change over time; see ICU ticket #12420.

Definition at line 250 of file numberformatter.h.

◆ UNumberSignDisplay

An enum declaring how to denote positive and negative numbers.

Example outputs when formatting 123 and -123 in en-US:

  • AUTO: "123", "-123"
  • ALWAYS: "+123", "-123"
  • NEVER: "123", "123"
  • ACCOUNTING: "$123", "($123)"
  • ACCOUNTING_ALWAYS: "+$123", "($123)"

The exact format, including the position and the code point of the sign, differ by locale.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_SIGN_ALWAYS 

Show the minus sign on negative numbers and the plus sign on positive numbers.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_SIGN_NEVER 

Do not show the sign on positive or negative numbers.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_SIGN_ACCOUNTING_ALWAYS 

Use the locale-dependent accounting format on negative numbers, and show the plus sign on positive numbers.

For more information on the accounting format, see the ACCOUNTING sign display strategy.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_SIGN_COUNT 

One more than the highest UNumberSignDisplay value.

Internal:
Do not use. This API is for internal use only. ICU 60: The numeric value may change over time; see ICU ticket #12420.

Definition at line 186 of file numberformatter.h.

◆ UNumberUnitWidth

An enum declaring how to render units, including currencies.

Example outputs when formatting 123 USD and 123 meters in en-CA:

  • NARROW*: "$123.00" and "123 m"
  • SHORT: "US$ 123.00" and "123 m"
  • FULL_NAME: "123.00 US dollars" and "123 meters"
  • ISO_CODE: "USD 123.00" and undefined behavior
  • HIDDEN: "123.00" and "123"
  • The narrow format for currencies is not currently supported; this is a known issue that will be fixed in a future version. See #11666 for more information.

This enum is similar to com.ibm.icu.text.MeasureFormat.FormatWidth.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
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.

Draft:
This API may be changed in the future versions and was introduced in ICU 60
UNUM_UNIT_WIDTH_COUNT 

One more than the highest UNumberUnitWidth value.

Internal:
Do not use. This API is for internal use only. ICU 60: The numeric value may change over time; see ICU ticket #12420.

Definition at line 99 of file numberformatter.h.