public final class NumberFormatter extends Object
// 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")) .rounding(Rounder.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) .rounding(Rounder.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 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(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.
Modifier and Type | Class and Description |
---|---|
static class |
NumberFormatter.DecimalSeparatorDisplay
An enum declaring how to render the decimal separator.
|
static class |
NumberFormatter.SignDisplay
An enum declaring how to denote positive and negative numbers.
|
static class |
NumberFormatter.UnitWidth
An enum declaring how to render units, including currencies.
|
Modifier and Type | Method and Description |
---|---|
static UnlocalizedNumberFormatter |
fromDecimalFormat(com.ibm.icu.impl.number.DecimalFormatProperties properties,
DecimalFormatSymbols symbols,
com.ibm.icu.impl.number.DecimalFormatProperties exportedProperties)
Deprecated.
ICU 60 This API is ICU internal only.
|
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.
|
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.
|
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.
|
public static UnlocalizedNumberFormatter with()
UnlocalizedNumberFormatter
, to be used for chaining.public static LocalizedNumberFormatter withLocale(Locale locale)
locale
- The locale from which to load formats and symbols for number formatting.LocalizedNumberFormatter
, to be used for chaining.public static LocalizedNumberFormatter withLocale(ULocale locale)
locale
- The locale from which to load formats and symbols for number formatting.LocalizedNumberFormatter
, to be used for chaining.@Deprecated public static UnlocalizedNumberFormatter fromDecimalFormat(com.ibm.icu.impl.number.DecimalFormatProperties properties, DecimalFormatSymbols symbols, com.ibm.icu.impl.number.DecimalFormatProperties exportedProperties)
Copyright ? 2016 Unicode, Inc. and others.