Class QuantityFormatter

java.lang.Object
com.ibm.icu.text.QuantityFormatter

class QuantityFormatter extends Object
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 Details

  • Constructor Details

    • QuantityFormatter

      public QuantityFormatter()
  • Method Details

    • addIfAbsent

      public void addIfAbsent(CharSequence variant, String template)
      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

      public String format(double number, NumberFormat numberFormat, PluralRules pluralRules)
      Format formats a number with this object.
      Parameters:
      number - the number to be formatted
      numberFormat - 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

      public SimpleFormatter getByVariant(CharSequence variant)
      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.