Usage example:
NumberRangeFormatter.with()
.identityFallback(RangeIdentityFallback.APPROXIMATELY_OR_SINGLE_VALUE)
.numberFormatterFirst(NumberFormatter.with().unit(MeasureUnit.METER))
.numberFormatterSecond(NumberFormatter.with().unit(MeasureUnit.KILOMETER))
.locale(ULocale.UK)
.formatRange(750, 1.2)
.toString();
// => "750 m - 1.2 km"
Like NumberFormatter, NumberRangeFormatter instances (i.e., LocalizedNumberRangeFormatter and UnlocalizedNumberRangeFormatter) are immutable and thread-safe. This API is based on the fluent design pattern popularized by libraries such as Google's Guava.
NumberFormatter
| Modifier and Type | Class and Description |
|---|---|
| public static enum | NumberRangeFormatter.RangeCollapse
Defines how to merge fields that are identical across the range sign. |
| public static enum | NumberRangeFormatter.RangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding. |
| public static enum | NumberRangeFormatter.RangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted in the range were equal or not, and whether or not the identity fallback was applied. |
| public static class | NumberRangeFormatter.SpanField
Class for span fields in FormattedNumberRange. |
| Modifier and Type | Field and Description |
|---|---|
| private static final UnlocalizedNumberRangeFormatter |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static UnlocalizedNumberRangeFormatter | Returns: AnUnlocalizedNumberRangeFormatter, to be used for chaining.Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is not currently known at the call site. |
| public static LocalizedNumberRangeFormatter | Returns: ALocalizedNumberRangeFormatter, to be used for chaining.The locale from which to load formats and symbols for number range formatting. locale)Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is known at the call site. |
| public static LocalizedNumberRangeFormatter | Returns: ALocalizedNumberRangeFormatter, to be used for chaining.The locale from which to load formats and symbols for number range formatting. locale)Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is known at the call site. |
| BASE | back to summary |
|---|---|
| private static final UnlocalizedNumberRangeFormatter BASE | |
| NumberRangeFormatter | back to summary |
|---|---|
| private NumberRangeFormatter() Private constructor - this class is not designed for instantiation | |
| with | back to summary |
|---|---|
| public static UnlocalizedNumberRangeFormatter with() Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is not currently known at the call site.
| |
| withLocale | back to summary |
|---|---|
| public static LocalizedNumberRangeFormatter withLocale(Locale locale) Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is known at the call site.
| |
| withLocale | back to summary |
|---|---|
| public static LocalizedNumberRangeFormatter withLocale(ULocale locale) Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is known at the call site.
| |
NumberRangeFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberRangeFormatter. | ALL
Collapse any field that is equal across the range sign. |
| public static final NumberRangeFormatter. | AUTO
Use locale data and heuristics to determine how much of the string to collapse. |
| public static final NumberRangeFormatter. | NONE
Do not collapse any part of the number. |
| public static final NumberRangeFormatter. | UNIT
Collapse the unit part of the number, but not the notation, if present. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberRangeFormatter. | |
| public static NumberRangeFormatter. |
| ALL | back to summary |
|---|---|
| public static final NumberRangeFormatter. Collapse any field that is equal across the range sign. May introduce ambiguity on the magnitude of the number. Example: "3.2 – 5.3 thousand kilograms"
| |
| AUTO | back to summary |
|---|---|
| public static final NumberRangeFormatter. Use locale data and heuristics to determine how much of the string to collapse. Could end up collapsing none, some, or all repeated pieces in a locale-sensitive way. The heuristics used for this option are subject to change over time.
| |
| NONE | back to summary |
|---|---|
| public static final NumberRangeFormatter. Do not collapse any part of the number. Example: "3.2 thousand kilograms – 5.3 thousand kilograms"
| |
| UNIT | back to summary |
|---|---|
| public static final NumberRangeFormatter. Collapse the unit part of the number, but not the notation, if present. Example: "3.2 thousand – 5.3 thousand kilograms"
| |
| RangeCollapse | back to summary |
|---|---|
| private RangeCollapse() | |
| valueOf | back to summary |
|---|---|
| public static NumberRangeFormatter. | |
| values | back to summary |
|---|---|
| public static NumberRangeFormatter. | |
NumberRangeFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberRangeFormatter. | APPROXIMATELY
Show the number using a locale-sensitive approximation pattern. |
| public static final NumberRangeFormatter. | APPROXIMATELY_OR_SINGLE_VALUE
Show the number using a locale-sensitive approximation pattern. |
| public static final NumberRangeFormatter. | RANGE
Show the number as the range of two equal values. |
| public static final NumberRangeFormatter. | SINGLE_VALUE
Show the number as a single value rather than a range. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberRangeFormatter. | |
| public static NumberRangeFormatter. |
| APPROXIMATELY | back to summary |
|---|---|
| public static final NumberRangeFormatter. Show the number using a locale-sensitive approximation pattern. Use the range pattern always, even if the inputs are the same. Example: "~$5"
| |
| APPROXIMATELY_OR_SINGLE_VALUE | back to summary |
|---|---|
| public static final NumberRangeFormatter. Show the number using a locale-sensitive approximation pattern. If the numbers were the same before rounding, show the single value. Example: "~$5" or "$5"
| |
| RANGE | back to summary |
|---|---|
| public static final NumberRangeFormatter. Show the number as the range of two equal values. Use the range pattern always, even if the inputs are the same. Example (with RangeCollapse.NONE): "$5 – $5"
| |
| SINGLE_VALUE | back to summary |
|---|---|
| public static final NumberRangeFormatter. Show the number as a single value rather than a range. Example: "$5"
| |
| RangeIdentityFallback | back to summary |
|---|---|
| private RangeIdentityFallback() | |
| valueOf | back to summary |
|---|---|
| public static NumberRangeFormatter. | |
| values | back to summary |
|---|---|
| public static NumberRangeFormatter. | |
NumberRangeFormatter
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberRangeFormatter. | EQUAL_AFTER_ROUNDING
Used to indicate that the two numbers in the range were equal, but only after rounding rules were applied. |
| public static final NumberRangeFormatter. | EQUAL_BEFORE_ROUNDING
Used to indicate that the two numbers in the range were equal, even before any rounding rules were applied. |
| public static final NumberRangeFormatter. | NOT_EQUAL
Used to indicate that the two numbers in the range were not equal, even after rounding rules were applied. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NumberRangeFormatter. | |
| public static NumberRangeFormatter. |
| EQUAL_AFTER_ROUNDING | back to summary |
|---|---|
| public static final NumberRangeFormatter. Used to indicate that the two numbers in the range were equal, but only after rounding rules were applied.
| |
| EQUAL_BEFORE_ROUNDING | back to summary |
|---|---|
| public static final NumberRangeFormatter. Used to indicate that the two numbers in the range were equal, even before any rounding rules were applied.
| |
| NOT_EQUAL | back to summary |
|---|---|
| public static final NumberRangeFormatter. Used to indicate that the two numbers in the range were not equal, even after rounding rules were applied.
| |
| RangeIdentityResult | back to summary |
|---|---|
| private RangeIdentityResult() | |
| valueOf | back to summary |
|---|---|
| public static NumberRangeFormatter. | |
| values | back to summary |
|---|---|
| public static NumberRangeFormatter. | |
| Modifier and Type | Field and Description |
|---|---|
| public static final NumberRangeFormatter. | NUMBER_RANGE_SPAN
The concrete field used for spans in FormattedNumberRange. |
| private static final long |
| Modifier and Type | Method and Description |
|---|---|
| protected Object | readResolve()
Overrides java.
Deprecated
This API is ICU internal only.
Serialization method resolve instances to the constant
NumberRangeFormatter.SpanField values
|
| NUMBER_RANGE_SPAN | back to summary |
|---|---|
| public static final NumberRangeFormatter. The concrete field used for spans in FormattedNumberRange. Instances of NUMBER_RANGE_SPAN should have an associated value, the index within the input list that is represented by the span.
| |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| SpanField | back to summary |
|---|---|
| private SpanField(String name) | |
| readResolve | back to summary |
|---|---|
| protected Object readResolve() throws InvalidObjectException Overrides java. Deprecated This API is ICU internal only. Serialization method resolve instances to the constant NumberRangeFormatter.SpanField values
| |