public abstract class NumberFormatterSettings<T extends NumberFormatterSettings<?>> extends Object
UnlocalizedNumberFormatter
and LocalizedNumberFormatter
. This class is not intended
for public subclassing.NumberFormatter
Modifier and Type | Method and Description |
---|---|
T |
decimal(NumberFormatter.DecimalSeparatorDisplay style)
Sets the decimal separator display strategy.
|
T |
displayOptions(DisplayOptions displayOptions)
Specifies the
DisplayOptions . |
boolean |
equals(Object other) |
T |
grouping(NumberFormatter.GroupingStrategy strategy)
Specifies the grouping strategy to use when formatting numbers.
|
int |
hashCode() |
T |
integerWidth(IntegerWidth style)
Specifies the minimum and maximum number of digits to render before the decimal mark.
|
T |
macros(com.ibm.icu.impl.number.MacroProps macros)
Deprecated.
ICU 60 This API is ICU internal only.
|
T |
notation(Notation notation)
Specifies the notation style (simple, scientific, or compact) for rendering numbers.
|
T |
padding(com.ibm.icu.impl.number.Padder padder)
Deprecated.
ICU 60 This API is ICU internal only.
|
T |
perUnit(MeasureUnit perUnit)
Sets a unit to be used in the denominator.
|
T |
precision(Precision precision)
Specifies the rounding precision to use when formatting numbers.
|
T |
roundingMode(RoundingMode roundingMode)
Specifies how to determine the direction to round a number when it has more digits than fit in the
desired precision.
|
T |
scale(Scale scale)
Sets a scale (multiplier) to be used to scale the number by an arbitrary amount before formatting.
|
T |
sign(NumberFormatter.SignDisplay style)
Sets the plus/minus sign display strategy.
|
T |
symbols(DecimalFormatSymbols symbols)
Specifies the symbols (decimal separator, grouping separator, percent sign, numerals, etc.) to use
when rendering numbers.
|
T |
symbols(NumberingSystem ns)
Specifies that the given numbering system should be used when fetching symbols.
|
T |
threshold(Long threshold)
Deprecated.
ICU 60 This API is ICU internal only.
|
String |
toSkeleton()
Creates a skeleton string representation of this number formatter.
|
T |
unit(MeasureUnit unit)
Specifies the unit (unit of measure, currency, or percent) to associate with rendered numbers.
|
T |
unitDisplayCase(String unitDisplayCase)
Deprecated.
This API is for technology preview only.
|
T |
unitWidth(NumberFormatter.UnitWidth style)
Sets the width of the unit (measure unit or currency).
|
T |
usage(String usage)
Specifies the usage for which numbers will be formatted ("person-height",
"road", "rainfall", etc.)
|
public T notation(Notation notation)
All notation styles will be properly localized with locale data, and all notation styles are compatible with units, rounding strategies, and other number formatter settings.
Pass this method the return value of a Notation
factory method. For example:
NumberFormatter.with().notation(Notation.compactShort())The default is to use simple notation.
notation
- The notation strategy to use.Notation
public T unit(MeasureUnit unit)
Note: The unit can also be specified by passing a Measure
to
LocalizedNumberFormatter.format(Measure)
. Units specified via the format method take
precedence over units specified here. This setter is designed for situations when the unit is
constant for the duration of the number formatting process.
All units will be properly localized with locale data, and all units are compatible with notation styles, rounding strategies, and other number formatter settings.
Pass this method any instance of MeasureUnit
. For units of measure:
NumberFormatter.with().unit(MeasureUnit.METER)Currency:
NumberFormatter.with().unit(Currency.getInstance("USD"))Percent:
NumberFormatter.with().unit(NoUnit.PERCENT)
See perUnit(com.ibm.icu.util.MeasureUnit)
for information on how to format strings like "5 meters per second".
The default is to render without units (equivalent to NoUnit.BASE
).
If the input usage is correctly set the output unit will change
according to usage
, locale
and unit
value.
unit
- The unit to render.MeasureUnit
,
Currency
,
NoUnit
,
perUnit(com.ibm.icu.util.MeasureUnit)
public T perUnit(MeasureUnit perUnit)
Pass this method any instance of MeasureUnit
. For example:
NumberFormatter.with().unit(MeasureUnit.METER).perUnit(MeasureUnit.SECOND)
The default is not to display any unit in the denominator.
If a per-unit is specified without a primary unit via unit(com.ibm.icu.util.MeasureUnit)
, the behavior is undefined.
perUnit
- The unit to render in the denominator.unit(com.ibm.icu.util.MeasureUnit)
public T precision(Precision precision)
Pass this method the return value of one of the factory methods on Precision
. For example:
NumberFormatter.with().precision(Precision.fixedFraction(2))
In most cases, the default rounding precision is to round to 6 fraction places; i.e.,
Precision.maxFraction(6)
. The exceptions are if compact notation is being used, then
the compact notation rounding precision is used (see Notation.compactShort()
for details), or
if the unit is a currency, then standard currency rounding is used, which varies from currency to
currency (see Precision.currency(com.ibm.icu.util.Currency.CurrencyUsage)
for details).
precision
- The rounding precision to use.Precision
public T roundingMode(RoundingMode roundingMode)
roundingMode
- The rounding mode to use.Precision
public T grouping(NumberFormatter.GroupingStrategy strategy)
The exact grouping widths will be chosen based on the locale.
Pass this method an element from the NumberFormatter.GroupingStrategy
enum. For example:
NumberFormatter.with().grouping(GroupingStrategy.MIN2)The default is to perform grouping according to locale data; most locales, but not all locales, enable it by default.
strategy
- The grouping strategy to use.NumberFormatter.GroupingStrategy
public T integerWidth(IntegerWidth style)
Pass this method the return value of IntegerWidth.zeroFillTo(int)
. For example:
NumberFormatter.with().integerWidth(IntegerWidth.zeroFillTo(2))The default is to have one minimum integer digit.
style
- The integer width to use.IntegerWidth
public T symbols(DecimalFormatSymbols symbols)
Pass this method an instance of DecimalFormatSymbols
. For example:
NumberFormatter.with().symbols(DecimalFormatSymbols.getInstance(new ULocale("de_CH")))
Note: DecimalFormatSymbols automatically chooses the best numbering system based on the locale. In the examples above, the first three are using the Latin numbering system, and the fourth is using the Myanmar numbering system.
Note: The instance of DecimalFormatSymbols will be copied: changes made to the symbols object after passing it into the fluent chain will not be seen.
Note: Calling this method will override the NumberingSystem previously specified
in symbols(NumberingSystem)
.
The default is to choose the symbols based on the locale specified in the fluent chain.
symbols
- The DecimalFormatSymbols to use.DecimalFormatSymbols
public T symbols(NumberingSystem ns)
Pass this method an instance of NumberingSystem
. For example, to force the locale to
always use the Latin alphabet numbering system (ASCII digits):
NumberFormatter.with().symbols(NumberingSystem.LATIN)
Note: Calling this method will override the DecimalFormatSymbols previously
specified in symbols(DecimalFormatSymbols)
.
The default is to choose the best numbering system for the locale.
ns
- The NumberingSystem to use.NumberingSystem
public T unitWidth(NumberFormatter.UnitWidth style)
Pass an element from the NumberFormatter.UnitWidth
enum to this setter. For example:
NumberFormatter.with().unitWidth(UnitWidth.FULL_NAME)
The default is the SHORT width.
style
- The width to use when rendering numbers.NumberFormatter.UnitWidth
public T sign(NumberFormatter.SignDisplay style)
Pass an element from the NumberFormatter.SignDisplay
enum to this setter. For example:
NumberFormatter.with().sign(SignDisplay.ALWAYS)
The default is AUTO sign display.
style
- The sign display strategy to use when rendering numbers.NumberFormatter.SignDisplay
public T decimal(NumberFormatter.DecimalSeparatorDisplay style)
Pass an element from the NumberFormatter.DecimalSeparatorDisplay
enum to this setter. For example:
NumberFormatter.with().decimal(DecimalSeparatorDisplay.ALWAYS)
The default is AUTO decimal separator display.
style
- The decimal separator display strategy to use when rendering numbers.NumberFormatter.DecimalSeparatorDisplay
public T scale(Scale scale)
Pass an element from a Scale
factory method to this setter. For example:
NumberFormatter.with().scale(Scale.powerOfTen(2))
The default is to not apply any multiplier.
scale
- An amount to be multiplied against numbers before formatting.Scale
public T usage(String usage)
When a usage
is specified, the output unit will change depending on the
Locale
and the unit quantity. For example, formatting length
measurements specified in meters:
NumberFormatter.with().usage("person").unit(MeasureUnit.METER).locale(new ULocale("en-US"))
The input unit specified via unit() determines the type of measurement being formatted (e.g. "length" when the unit is "foot"). The usage requested will be looked for only within this category of measurement units.
The output unit can be found via FormattedNumber.getOutputUnit().
If the usage has multiple parts (e.g. "land-agriculture-grain") and does not match a known usage preference, the last part will be dropped repeatedly until a match is found (e.g. trying "land-agriculture", then "land"). If a match is still not found, usage will fall back to "default".
Setting usage to an empty string clears the usage (disables usage-based localized formatting).
Setting a usage string but not a correct input unit will result in an U_ILLEGAL_ARGUMENT_ERROR.
When using usage, specifying rounding or precision is unnecessary. Specifying a precision in some manner will override the default formatting.
usage
- A usage parameter from the units resource.IllegalArgumentException
- in case of Setting a usage string but not a correct input unit.public T displayOptions(DisplayOptions displayOptions)
DisplayOptions
. For example, GrammaticalCase
specifies
the desired case for a unit formatter's output (e.g. accusative, dative, genitive).@Deprecated public T unitDisplayCase(String unitDisplayCase)
@Deprecated public T macros(com.ibm.icu.impl.number.MacroProps macros)
@Deprecated public T padding(com.ibm.icu.impl.number.Padder padder)
@Deprecated public T threshold(Long threshold)
public String toSkeleton()
Not all options are capable of being represented in the skeleton string; for example, a
DecimalFormatSymbols object. If any such option is encountered, an
UnsupportedOperationException
is thrown.
The returned skeleton is in normalized form, such that two number formatters with equivalent behavior should produce the same skeleton.
For more information on number skeleton strings, see: https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html
UnsupportedOperationException
- If the number formatter has an option that cannot be represented in a skeleton string.Copyright © 2016 Unicode, Inc. and others.