Package com.ibm.icu.text
Class QuantityFormatter
java.lang.Object
com.ibm.icu.text.QuantityFormatter
QuantityFormatter represents an unknown quantity of something and formats a known quantity
in terms of that something. For example, a QuantityFormatter that represents X apples may
format 1 as "1 apple" and 3 as "3 apples"
QuanitityFormatter appears here instead of in com.ibm.icu.impl because it depends on PluralRules and DecimalFormat. It is package-protected as it is not meant for public use.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addIfAbsent
(CharSequence variant, String template) Adds a template if there is none yet for the plural form.format
(double number, NumberFormat numberFormat, PluralRules pluralRules) Format formats a number with this object.static StringBuilder
format
(String compiledPattern, CharSequence value, StringBuilder appendTo, FieldPosition pos) Formats the pattern with the value and adjusts the FieldPosition.getByVariant
(CharSequence variant) Gets the SimpleFormatter for a particular variant.boolean
isValid()
static StandardPlural
selectPlural
(double number, NumberFormat numberFormat, PluralRules rules) Selects the standard plural form for the number/formatter/rules.
-
Field Details
-
templates
-
-
Constructor Details
-
QuantityFormatter
public QuantityFormatter()
-
-
Method Details
-
addIfAbsent
Adds a template if there is none yet for the plural form.- Parameters:
variant
- the plural variant, e.g "zero", "one", "two", "few", "many", "other"template
- the text for that plural variant with "{0}" as the quantity. For example, in English, the template for the "one" variant may be "{0} apple" while the template for the "other" variant may be "{0} apples"- Throws:
IllegalArgumentException
- if variant is not recognized or if template has more than just the {0} placeholder.
-
isValid
public boolean isValid()- Returns:
- true if this object has at least the "other" variant
-
format
Format formats a number with this object.- Parameters:
number
- the number to be formattednumberFormat
- used to actually format the number.pluralRules
- uses the number and the numberFormat to determine what plural variant to use for fetching the formatting template.- Returns:
- the formatted string e.g '3 apples'
-
getByVariant
Gets the SimpleFormatter for a particular variant.- Parameters:
variant
- "zero", "one", "two", "few", "many", "other"- Returns:
- the SimpleFormatter
-
selectPlural
public static StandardPlural selectPlural(double number, NumberFormat numberFormat, PluralRules rules) Selects the standard plural form for the number/formatter/rules. -
format
public static StringBuilder format(String compiledPattern, CharSequence value, StringBuilder appendTo, FieldPosition pos) Formats the pattern with the value and adjusts the FieldPosition.
-