NumberFormatterSettings.
// 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(Currency.getInstance("EUR"))
.precision(Precision.maxDigits(2))
.locale(...)
.format(1234)
.toString(); // €1.2K in en-US
// Create a formatter in a private static final field:
private static final LocalizedNumberFormatter formatter = NumberFormatter.withLocale(...)
.unit(NoUnit.PERCENT)
.precision(Precision.fixedFraction(3));
formatter.format(5.9831).toString(); // 5.983% in en-US
// Create a "template" in a private static final field but without setting a locale until the call site:
private static final UnlocalizedNumberFormatter template = NumberFormatter.with()
.sign(SignDisplay.ALWAYS)
.unitWidth(UnitWidth.FULL_NAME);
template.locale(...).format(new Measure(1234, MeasureUnit.METER)).toString(); // +1,234 meters in en-US
This API offers more features than com. and is geared toward new
users of ICU.
NumberFormatter instances (i.e., LocalizedNumberFormatter and UnlocalizedNumberFormatter) 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.precision(Precision.maxFraction(2)); // does nothing!
formatter.locale(ULocale.ENGLISH).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.
Note
To format monetary/currency values, specify the currency in the `.unit()` function.
| Modifier and Type | Class and Description |
|---|---|
| public static enum | NumberFormatter.DecimalSeparatorDisplay
An enum declaring how to render the decimal separator. |
| public static enum | NumberFormatter.GroupingStrategy
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). |
| public static enum | NumberFormatter.RoundingPriority
An enum declaring how to resolve conflicts between maximum fraction digits and maximum significant digits. |
| public static enum | NumberFormatter.SignDisplay
An enum declaring how to denote positive and negative numbers. |
| public static enum | NumberFormatter.TrailingZeroDisplay
An enum declaring how to render trailing zeros. |
| public static enum | NumberFormatter.UnitWidth
An enum declaring how to render units, including currencies. |
| Modifier and Type | Field and Description |
|---|---|
| private static final UnlocalizedNumberFormatter | |
| pack-priv static final long | DEFAULT_THRESHOLD
Use a default threshold of 3. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static UnlocalizedNumberFormatter | Returns: AnUnlocalizedNumberFormatter, to be used for chaining.The skeleton string off of which to base this NumberFormatter. skeleton)Call this method at the beginning of a NumberFormatter fluent chain to create an instance based on a given number skeleton string. |
| public static UnlocalizedNumberFormatter | fromDecimalFormat(DecimalFormatProperties properties, DecimalFormatSymbols symbols, DecimalFormatProperties exportedProperties)
Deprecated
ICU 60 This API is ICU internal only.
Note In Java, since NumberPropertyMapper is package-private, this method is here so that it is accessible to tests. |
| public static UnlocalizedNumberFormatter | Returns: AnUnlocalizedNumberFormatter, to be used for chaining.Call this method at the beginning of a NumberFormatter fluent chain in which the locale is not currently known at the call site. |
| public static LocalizedNumberFormatter | Returns: ALocalizedNumberFormatter, to be used for chaining.The locale from which to load formats and symbols for number formatting. locale)Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at the call site. |
| public static LocalizedNumberFormatter | Returns: ALocalizedNumberFormatter, to be used for chaining.The locale from which to load formats and symbols for number formatting. locale)Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at the call site. |
| BASE | back to summary |
|---|---|
| private static final UnlocalizedNumberFormatter BASE | |
| DEFAULT_THRESHOLD | back to summary |
|---|---|
| pack-priv static final long DEFAULT_THRESHOLD Use a default threshold of 3. This means that the third time .format() is called, the data structures get built using the "safe" code path. The first two calls to .format() will trigger the unsafe code path. | |
| NumberFormatter | back to summary |
|---|---|
| private NumberFormatter() Private constructor, not designed for instantiation. | |
| forSkeleton | back to summary |
|---|---|
| public static UnlocalizedNumberFormatter forSkeleton(String skeleton) Call this method at the beginning of a NumberFormatter fluent chain to create an instance based on a given number skeleton string. For more information on number skeleton strings, see: https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html
| |
| fromDecimalFormat | back to summary |
|---|---|
| public static UnlocalizedNumberFormatter fromDecimalFormat(DecimalFormatProperties properties, DecimalFormatSymbols symbols, DecimalFormatProperties exportedProperties)
Deprecated ICU 60 This API is ICU internal only. Note In Java, since NumberPropertyMapper is package-private, this method is here so that it is accessible to tests.
| |
| with | back to summary |
|---|---|
| public static UnlocalizedNumberFormatter with() Call this method at the beginning of a NumberFormatter fluent chain in which the locale is not currently known at the call site.
| |
| withLocale | back to summary |
|---|---|
| public static LocalizedNumberFormatter withLocale(Locale locale) Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at the call site.
| |
| withLocale | back to summary |
|---|---|
| public static LocalizedNumberFormatter withLocale(ULocale locale) Call this method at the beginning of a NumberFormatter fluent chain in which the locale is known at the call site.
| |
NumberFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberFormatter. | ALWAYS
Always show the decimal separator, even if there are no digits to display after the separator. |
| public static final NumberFormatter. | AUTO
Show the decimal separator when there are one or more digits to display after the separator, and do not show it otherwise. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberFormatter. | |
| public static NumberFormatter. |
| ALWAYS | back to summary |
|---|---|
| public static final NumberFormatter. Always show the decimal separator, even if there are no digits to display after the separator.
| |
| AUTO | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| DecimalSeparatorDisplay | back to summary |
|---|---|
| private DecimalSeparatorDisplay() | |
| valueOf | back to summary |
|---|---|
| public static NumberFormatter. | |
| values | back to summary |
|---|---|
| public static NumberFormatter. | |
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.
NumberFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberFormatter. | AUTO
Display grouping using the default strategy for all locales. |
| public static final NumberFormatter. | 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). |
| public static final NumberFormatter. | OFF
Do not display grouping separators in any locale. |
| public static final NumberFormatter. | ON_ALIGNED
Always display the grouping separator on values of at least 1000. |
| public static final NumberFormatter. | THOUSANDS
Use the Western defaults: groups of 3 and enabled for all numbers 1000 or greater. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberFormatter. | |
| public static NumberFormatter. |
| AUTO | back to summary |
|---|---|
| public static final NumberFormatter. 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).
| |
| MIN2 | back to summary |
|---|---|
| public static final NumberFormatter. 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).
| |
| OFF | back to summary |
|---|---|
| public static final NumberFormatter. Do not display grouping separators in any locale.
| |
| ON_ALIGNED | back to summary |
|---|---|
| public static final NumberFormatter. 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).
| |
| THOUSANDS | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| GroupingStrategy | back to summary |
|---|---|
| private GroupingStrategy() | |
| valueOf | back to summary |
|---|---|
| public static NumberFormatter. | |
| values | back to summary |
|---|---|
| public static NumberFormatter. | |
There are two modes, RELAXED and STRICT:
The default settings for compact notation rounding are Max-Fraction = 0 (round to the nearest integer), Max-Significant = 2 (round to 2 significant digits), and priority RELAXED (choose the constraint that results in more digits being displayed).
Conflicting minimum fraction and significant digits are always resolved in the direction that results in more trailing zeros.
Example 1: Consider the number 3.141, with various different settings:
The rounding priority determines how to resolve the conflict when both Max-Fraction and Max-Significant are set. With RELAXED, the less-strict setting (the one that causes more digits to be displayed) will be used; Max-Significant wins. With STRICT, the more-strict setting (the one that causes fewer digits to be displayed) will be used; Max-Fraction wins.
Example 2: Consider the number 8317, with various different settings:
Here, RELAXED favors Max-Fraction and STRICT favors Max-Significant. Note that this larger number caused the two modes to favor the opposite result.
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberFormatter. | RELAXED
Favor greater precision by relaxing one of the rounding constraints. |
| public static final NumberFormatter. | STRICT
Favor adherence to all rounding constraints by producing lower precision. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberFormatter. | |
| public static NumberFormatter. |
| RELAXED | back to summary |
|---|---|
| public static final NumberFormatter. Favor greater precision by relaxing one of the rounding constraints.
| |
| STRICT | back to summary |
|---|---|
| public static final NumberFormatter. Favor adherence to all rounding constraints by producing lower precision.
| |
| RoundingPriority | back to summary |
|---|---|
| private RoundingPriority() | |
| valueOf | back to summary |
|---|---|
| public static NumberFormatter. | |
| values | back to summary |
|---|---|
| public static NumberFormatter. | |
The exact format, including the position and the code point of the sign, differ by locale.
NumberFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberFormatter. | ACCOUNTING
Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. |
| public static final NumberFormatter. | ACCOUNTING_ALWAYS
Use the locale-dependent accounting format on negative numbers, and show the plus sign on positive numbers, including zero. |
| public static final NumberFormatter. | ACCOUNTING_EXCEPT_ZERO
Use the locale-dependent accounting format on negative numbers, and show the plus sign on positive numbers. |
| public static final NumberFormatter. | ACCOUNTING_NEGATIVE
Same as ACCOUNTING, but do not show the sign on negative zero. |
| public static final NumberFormatter. | ALWAYS
Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. |
| public static final NumberFormatter. | AUTO
Show the minus sign on negative numbers, and do not show the sign on positive numbers. |
| public static final NumberFormatter. | EXCEPT_ZERO
Show the minus sign on negative numbers and the plus sign on positive numbers. |
| public static final NumberFormatter. | NEGATIVE
Same as AUTO, but do not show the sign on negative zero. |
| public static final NumberFormatter. | NEVER
Do not show the sign on positive or negative numbers. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberFormatter. | |
| public static NumberFormatter. |
| ACCOUNTING | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| ACCOUNTING_ALWAYS | back to summary |
|---|---|
| public static final NumberFormatter. 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
| |
| ACCOUNTING_EXCEPT_ZERO | back to summary |
|---|---|
| public static final NumberFormatter. Use the locale-dependent accounting format on negative numbers, and show the plus sign on positive numbers. Do not show a sign on zero, numbers that round to zero, or NaN. For more information on the accounting format, see the ACCOUNTING sign display strategy.
| |
| ACCOUNTING_NEGATIVE | back to summary |
|---|---|
| public static final NumberFormatter. Same as ACCOUNTING, but do not show the sign on negative zero.
| |
| ALWAYS | back to summary |
|---|---|
| public static final NumberFormatter. Show the minus sign on negative numbers and the plus sign on positive numbers, including zero.
To hide the sign on zero, see
| |
| AUTO | back to summary |
|---|---|
| public static final NumberFormatter. Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default behavior. If using this option, a sign will be displayed on negative zero, including negative numbers that round to zero. To hide the sign on negative zero, use the NEGATIVE option.
| |
| EXCEPT_ZERO | back to summary |
|---|---|
| public static final NumberFormatter. Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a sign on zero, numbers that round to zero, or NaN.
| |
| NEGATIVE | back to summary |
|---|---|
| public static final NumberFormatter. Same as AUTO, but do not show the sign on negative zero.
| |
| NEVER | back to summary |
|---|---|
| public static final NumberFormatter. Do not show the sign on positive or negative numbers.
| |
| SignDisplay | back to summary |
|---|---|
| private SignDisplay() | |
| valueOf | back to summary |
|---|---|
| public static NumberFormatter. | |
| values | back to summary |
|---|---|
| public static NumberFormatter. | |
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberFormatter. | AUTO
Display trailing zeros according to the settings for minimum fraction and significant digits. |
| public static final NumberFormatter. | HIDE_IF_WHOLE
Same as AUTO, but hide trailing zeros after the decimal separator if they are all zero. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberFormatter. | |
| public static NumberFormatter. |
| AUTO | back to summary |
|---|---|
| public static final NumberFormatter. Display trailing zeros according to the settings for minimum fraction and significant digits.
| |
| HIDE_IF_WHOLE | back to summary |
|---|---|
| public static final NumberFormatter. Same as AUTO, but hide trailing zeros after the decimal separator if they are all zero.
| |
| TrailingZeroDisplay | back to summary |
|---|---|
| private TrailingZeroDisplay() | |
| valueOf | back to summary |
|---|---|
| public static NumberFormatter. | |
| values | back to summary |
|---|---|
| public static NumberFormatter. | |
This enum is similar to com..
NumberFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberFormatter. | FORMAL
Use the formal variant of the currency symbol; for example, "NT$" for the New Taiwan dollar in zh-TW. |
| public static final NumberFormatter. | FULL_NAME
Print the full name of the unit, without any abbreviations. |
| public static final NumberFormatter. | HIDDEN
Format the number according to the specified unit, but do not display the unit. |
| public static final NumberFormatter. | ISO_CODE
Use the three-digit ISO XXX code in place of the symbol for displaying currencies. |
| public static final NumberFormatter. | NARROW
Print an abbreviated version of the unit name. |
| public static final NumberFormatter. | SHORT
Print an abbreviated version of the unit name. |
| public static final NumberFormatter. | VARIANT
Use the alternate variant of the currency symbol; for example, "TL" for the Turkish lira (TRY). |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberFormatter. | |
| public static NumberFormatter. |
| FORMAL | back to summary |
|---|---|
| public static final NumberFormatter. Use the formal variant of the currency symbol; for example, "NT$" for the New Taiwan dollar in zh-TW. Behavior of this option with non-currency units is not defined at this time.
| |
| FULL_NAME | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| HIDDEN | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| ISO_CODE | back to summary |
|---|---|
| public static final NumberFormatter. Use the three-digit ISO XXX code in place of the symbol for displaying currencies. Behavior of this option with non-currency units is not defined at this time. In CLDR, this option corresponds to the "¤¤" placeholder for currencies.
| |
| NARROW | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| SHORT | back to summary |
|---|---|
| public static final NumberFormatter. 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.
| |
| VARIANT | back to summary |
|---|---|
| public static final NumberFormatter. Use the alternate variant of the currency symbol; for example, "TL" for the Turkish lira (TRY). Behavior of this option with non-currency units is not defined at this time.
| |
| UnitWidth | back to summary |
|---|---|
| private UnitWidth() | |
| valueOf | back to summary |
|---|---|
| public static NumberFormatter. | |
| values | back to summary |
|---|---|
| public static NumberFormatter. | |