Added in API level 1

ChoiceFormat

open class ChoiceFormat : NumberFormat

A ChoiceFormat allows you to attach a format to a range of numbers. It is generally used in a MessageFormat for handling plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item:

X matches j if and only if limit[j]  X < limit[j+1]
  
If there is no match, then either the first or last index is used, depending on whether the number (X) is too low or too high. If the limit array is not in ascending order, the results of formatting will be incorrect. ChoiceFormat also accepts \u221E as equivalent to infinity(INF).

Note: ChoiceFormat differs from the other Format classes in that you create a ChoiceFormat object with a constructor (not with a getInstance style factory method). The factory methods aren't necessary because ChoiceFormat doesn't require any complex setup for a given locale. In fact, ChoiceFormat doesn't implement any locale specific behavior.

When creating a ChoiceFormat, you must specify an array of formats and an array of limits. The length of these arrays must be the same. For example,

  • limits = {1,2,3,4,5,6,7}
    formats = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
  • limits = {0, 1, ChoiceFormat.nextDouble(1)}
    formats = {"no files", "one file", "many files"}
    (nextDouble can be used to get the next higher double, to make the half-open interval.)

Here is a simple example that shows formatting and parsing:

<code>double[] limits = {1,2,3,4,5,6,7};
  String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
  ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
  ParsePosition status = new ParsePosition(0);
  for (double i = 0.0; i &lt;= 8.0; ++i) {
      status.setIndex(0);
      System.out.println(i + " -&gt; " + form.format(i) + " -&gt; "
                               + form.parse(form.format(i),status));
  }
  </code>
Here is a more complex example, with a pattern format:
<code>double[] filelimits = {0,1,2};
  String[] filepart = {"are no files","is one file","are {2} files"};
  ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
  Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
  MessageFormat pattform = new MessageFormat("There {0} on {1}");
  pattform.setFormats(testFormats);
  Object[] testArgs = {null, "ADisk", null};
  for (int i = 0; i &lt; 4; ++i) {
      testArgs[0] = new Integer(i);
      testArgs[2] = testArgs[0];
      System.out.println(pattform.format(testArgs));
  }
  </code>

Specifying a pattern for ChoiceFormat objects is fairly straightforward. For example:

<code>ChoiceFormat fmt = new ChoiceFormat(
       "-1#is negative| 0#is zero or fraction | 1#is one |1.0&lt;is 1+ |2#is two |2&lt;is more than 2.");
  System.out.println("Formatter Pattern : " + fmt.toPattern());
 
  System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
  System.out.println("Format with -1.0 : " + fmt.format(-1.0));
  System.out.println("Format with 0 : " + fmt.format(0));
  System.out.println("Format with 0.9 : " + fmt.format(0.9));
  System.out.println("Format with 1.0 : " + fmt.format(1));
  System.out.println("Format with 1.5 : " + fmt.format(1.5));
  System.out.println("Format with 2 : " + fmt.format(2));
  System.out.println("Format with 2.1 : " + fmt.format(2.1));
  System.out.println("Format with NaN : " + fmt.format(Double.NaN));
  System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
  </code>
And the output result would be like the following:
<code>Format with -INF : is negative
  Format with -1.0 : is negative
  Format with 0 : is zero or fraction
  Format with 0.9 : is zero or fraction
  Format with 1.0 : is one
  Format with 1.5 : is 1+
  Format with 2 : is two
  Format with 2.1 : is more than 2.
  Format with NaN : is negative
  Format with +INF : is more than 2.
  </code>

Synchronization

Choice formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Summary

Inherited constants
Int FRACTION_FIELD

Field constant used to construct a FieldPosition object. Signifies that the position of the fraction part of a formatted number should be returned.

Int INTEGER_FIELD

Field constant used to construct a FieldPosition object. Signifies that the position of the integer part of a formatted number should be returned.

Public constructors
ChoiceFormat(limits: DoubleArray!, formats: Array<String!>!)

Constructs with the limits and the corresponding formats.

ChoiceFormat(newPattern: String!)

Constructs with limits and corresponding formats based on the pattern.

Public methods
open Unit
applyPattern(newPattern: String!)

Sets the pattern.

open Any

Overrides Cloneable

open Boolean
equals(other: Any?)

Equality comparison between two

open StringBuffer
format(number: Double, toAppendTo: StringBuffer, status: FieldPosition)

Returns pattern with formatted double.

open StringBuffer
format(number: Long, toAppendTo: StringBuffer, status: FieldPosition)

Specialization of format.

open Array<Any!>!

open DoubleArray!

open Int

Generates a hash code for the message format object.

static Double

Finds the least double greater than d.

open static Double
nextDouble(d: Double, positive: Boolean)

Finds the least double greater than d (if positive is true), or the greatest double less than d (if positive is false).

open Number?
parse(text: String, status: ParsePosition)

Parses a Number from the input text.

static Double

Finds the greatest double less than d.

open Unit
setChoices(limits: DoubleArray!, formats: Array<String!>!)

Set the choices to be used in formatting.

open String!

Gets the pattern.

Inherited functions
String format(number: Double)

Specialization of format.

StringBuffer format(number: Any, toAppendTo: StringBuffer, pos: FieldPosition)

Formats a number and appends the resulting text to the given string buffer. The number can be of any subclass of java.lang.Number.

This implementation extracts the number's value using java.lang.Number#longValue() for all integral type values that can be converted to long without loss of information, including BigInteger values with a bit length of less than 64, and java.lang.Number#doubleValue() for all other types. It then calls format(long,java.lang.StringBuffer,java.text.FieldPosition) or format(double,java.lang.StringBuffer,java.text.FieldPosition). This may result in loss of magnitude information and precision for BigInteger and BigDecimal values.

String format(number: Long)

Specialization of format.

Array<Locale!> getAvailableLocales()

Returns a scientific format for the current default locale. /*public* static final NumberFormat getScientificInstance() { return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, SCIENTIFICSTYLE); } /** Returns a scientific format for the specified locale.

Currency? getCurrency()

Gets the currency used by this number format when formatting currency values. The initial value is derived in a locale dependent way. The returned value may be null if no valid currency could be determined and no currency has been set using setCurrency.

The default implementation throws UnsupportedOperationException.

NumberFormat getCurrencyInstance()

Returns a currency format for the current default FORMAT locale.

This is equivalent to calling getCurrencyInstance(Locale.getDefault(Locale.Category.FORMAT)).

NumberFormat getCurrencyInstance(inLocale: Locale)

Returns a currency format for the specified locale.

If the specified locale contains the "cf" ( currency format style) Unicode extension, the returned currency format uses the style if it is available. Otherwise, the style uses the default "standard" currency format. For example, if the style designates "account", negative currency amounts use a pair of parentheses in some locales.

NumberFormat getInstance()

Returns a general-purpose number format for the current default FORMAT locale. This is the same as calling getNumberInstance().

NumberFormat getInstance(inLocale: Locale)

Returns a general-purpose number format for the specified locale. This is the same as calling getNumberInstance(inLocale).

NumberFormat getIntegerInstance()

Returns an integer number format for the current default FORMAT locale. The returned number format is configured to round floating point numbers to the nearest integer using half-even rounding (see RoundingMode.HALF_EVEN) for formatting, and to parse only the integer part of an input string (see isParseIntegerOnly).

This is equivalent to calling getIntegerInstance(Locale.getDefault(Locale.Category.FORMAT)).

NumberFormat getIntegerInstance(inLocale: Locale)

Returns an integer number format for the specified locale. The returned number format is configured to round floating point numbers to the nearest integer using half-even rounding (see RoundingMode.HALF_EVEN) for formatting, and to parse only the integer part of an input string (see isParseIntegerOnly).

Int getMaximumFractionDigits()

Returns the maximum number of digits allowed in the fraction portion of a number.

Int getMaximumIntegerDigits()

Returns the maximum number of digits allowed in the integer portion of a number.

Int getMinimumFractionDigits()

Returns the minimum number of digits allowed in the fraction portion of a number.

Int getMinimumIntegerDigits()

Returns the minimum number of digits allowed in the integer portion of a number.

NumberFormat getNumberInstance()

Returns a general-purpose number format for the current default FORMAT locale.

This is equivalent to calling getNumberInstance(Locale.getDefault(Locale.Category.FORMAT)).

NumberFormat getNumberInstance(inLocale: Locale)

Returns a general-purpose number format for the specified locale.

NumberFormat getPercentInstance()

Returns a percentage format for the current default FORMAT locale.

This is equivalent to calling getPercentInstance(Locale.getDefault(Locale.Category.FORMAT)).

NumberFormat getPercentInstance(inLocale: Locale)

Returns a percentage format for the specified locale.

RoundingMode getRoundingMode()

Gets the java.math.RoundingMode used in this NumberFormat. The default implementation of this method in NumberFormat always throws java.lang.UnsupportedOperationException. Subclasses which handle different rounding modes should override this method.

Boolean isGroupingUsed()

Returns true if grouping is used in this format. For example, in the English locale, with grouping on, the number 1234567 might be formatted as "1,234,567". The grouping separator as well as the size of each group is locale dependent and is determined by sub-classes of NumberFormat.

Boolean isParseIntegerOnly()

Returns true if this format will parse numbers as integers only. For example in the English locale, with ParseIntegerOnly true, the string "1234." would be parsed as the integer value 1234 and parsing would stop at the "." character. Of course, the exact format accepted by the parse operation is locale dependent and determined by sub-classes of NumberFormat.

Number? parse(source: String)

Parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.

See the parse(java.lang.String,java.text.ParsePosition) method for more information on number parsing.

WARNING: Don't use this method to deserialize a number. The underlying localized number format and parsing behaviors can change across Android versions as common usage in the locale changes. Consider using the parse methods in the boxed types, e.g. Long.parseLong(String), or java.math.BigDecimal for deserializing a locale-independent decimal number.

Any? parseObject(source: String, pos: ParsePosition)

Parses text from a string to produce a Number.

The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed number is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

See the parse(java.lang.String,java.text.ParsePosition) method for more information on number parsing.

Unit setCurrency(currency: Currency)

Sets the currency used by this number format when formatting currency values. This does not update the minimum or maximum number of fraction digits used by the number format.

The default implementation throws UnsupportedOperationException.

Unit setGroupingUsed(newValue: Boolean)

Set whether or not grouping will be used in this format.

Unit setMaximumFractionDigits(newValue: Int)

Sets the maximum number of digits allowed in the fraction portion of a number. maximumFractionDigits must be ≥ minimumFractionDigits. If the new value for maximumFractionDigits is less than the current value of minimumFractionDigits, then minimumFractionDigits will also be set to the new value.

Unit setMaximumIntegerDigits(newValue: Int)

Sets the maximum number of digits allowed in the integer portion of a number. maximumIntegerDigits must be ≥ minimumIntegerDigits. If the new value for maximumIntegerDigits is less than the current value of minimumIntegerDigits, then minimumIntegerDigits will also be set to the new value.

Unit setMinimumFractionDigits(newValue: Int)

Sets the minimum number of digits allowed in the fraction portion of a number. minimumFractionDigits must be ≤ maximumFractionDigits. If the new value for minimumFractionDigits exceeds the current value of maximumFractionDigits, then maximumFractionDigits will also be set to the new value

Unit setMinimumIntegerDigits(newValue: Int)

Sets the minimum number of digits allowed in the integer portion of a number. minimumIntegerDigits must be ≤ maximumIntegerDigits. If the new value for minimumIntegerDigits exceeds the current value of maximumIntegerDigits, then maximumIntegerDigits will also be set to the new value

Unit setParseIntegerOnly(value: Boolean)

Sets whether or not numbers should be parsed as integers only.

Unit setRoundingMode(roundingMode: RoundingMode?)

Sets the java.math.RoundingMode used in this NumberFormat. The default implementation of this method in NumberFormat always throws java.lang.UnsupportedOperationException. Subclasses which handle different rounding modes should override this method.

String! format(obj: Any!)

Formats an object to produce a string. This is equivalent to format(obj, new StringBuffer(), new FieldPosition(0)).toString();

AttributedCharacterIterator! formatToCharacterIterator(obj: Any!)

Formats an Object producing an AttributedCharacterIterator. You can use the returned AttributedCharacterIterator to build the resulting String, as well as to determine information about the resulting String.

Each attribute key of the AttributedCharacterIterator will be of type Field. It is up to each Format implementation to define what the legal values are for each attribute in the AttributedCharacterIterator, but typically the attribute key is also used as the attribute value.

The default implementation creates an AttributedCharacterIterator with no attributes. Subclasses that support fields should override this and create an AttributedCharacterIterator with meaningful attributes.

Any! parseObject(source: String!)

Parses text from the beginning of the given string to produce an object. The method may not use the entire text of the given string.

Public constructors

ChoiceFormat

Added in API level 1
ChoiceFormat(
    limits: DoubleArray!,
    formats: Array<String!>!)

Constructs with the limits and the corresponding formats.

Parameters
limits DoubleArray!: limits in ascending order
formats Array<String!>!: corresponding format strings
Exceptions
java.lang.NullPointerException if limits or formats is null

See Also

ChoiceFormat

Added in API level 1
ChoiceFormat(newPattern: String!)

Constructs with limits and corresponding formats based on the pattern.

Parameters
newPattern String!: the new pattern string
Exceptions
java.lang.NullPointerException if newPattern is null

See Also

Public methods

applyPattern

Added in API level 1
open fun applyPattern(newPattern: String!): Unit

Sets the pattern.

Parameters
newPattern String!: See the class description.
Exceptions
java.lang.NullPointerException if newPattern is null

clone

Added in API level 1
open fun clone(): Any

Overrides Cloneable

Return
Any a clone of this instance.
Exceptions
java.lang.CloneNotSupportedException if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

equals

Added in API level 1
open fun equals(other: Any?): Boolean

Equality comparison between two

Parameters
obj the reference object with which to compare.
Return
Boolean true if this object is the same as the obj argument; false otherwise.

format

Added in API level 1
open fun format(
    number: Double,
    toAppendTo: StringBuffer,
    status: FieldPosition
): StringBuffer

Returns pattern with formatted double.

Parameters
number Double: number to be formatted and substituted.
toAppendTo StringBuffer: where text is appended.
pos keeps track on the position of the field within the returned string. For example, for formatting a number 1234567.89 in Locale.US locale, if the given fieldPosition is NumberFormat.INTEGER_FIELD, the begin index and end index of fieldPosition will be set to 0 and 9, respectively for the output string 1,234,567.89.
status FieldPosition: ignore no useful status is returned.
Return
StringBuffer the formatted StringBuffer
Exceptions
java.lang.ArithmeticException if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY
java.lang.NullPointerException if toAppendTo is null

format

Added in API level 1
open fun format(
    number: Long,
    toAppendTo: StringBuffer,
    status: FieldPosition
): StringBuffer

Specialization of format. This method really calls format(double, StringBuffer, FieldPosition) thus the range of longs that are supported is only equal to the range that can be stored by double. This will never be a practical limitation.

Parameters
number Long: the long number to format
toAppendTo StringBuffer: the StringBuffer to which the formatted text is to be appended
pos keeps track on the position of the field within the returned string. For example, for formatting a number 123456789 in Locale.US locale, if the given fieldPosition is NumberFormat.INTEGER_FIELD, the begin index and end index of fieldPosition will be set to 0 and 11, respectively for the output string 123,456,789.
Return
StringBuffer the formatted StringBuffer
Exceptions
java.lang.ArithmeticException if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY

getFormats

Added in API level 1
open fun getFormats(): Array<Any!>!
Return
Array<Any!>! a copy of the String[] array supplied to the constructor or the most recent call to setChoices(double[],java.lang.String[]).

getLimits

Added in API level 1
open fun getLimits(): DoubleArray!
Return
DoubleArray! a copy of the double[] array supplied to the constructor or the most recent call to setChoices(double[],java.lang.String[]).

hashCode

Added in API level 1
open fun hashCode(): Int

Generates a hash code for the message format object.

Return
Int a hash code value for this object.

nextDouble

Added in API level 1
static fun nextDouble(d: Double): Double

Finds the least double greater than d. If NaN, returns same value.

Used to make half-open intervals.

Parameters
d Double: the reference value
Return
Double the least double value greather than d

See Also

nextDouble

Added in API level 1
open static fun nextDouble(
    d: Double,
    positive: Boolean
): Double

Finds the least double greater than d (if positive is true), or the greatest double less than d (if positive is false). If NaN, returns same value.

Parameters
d Double: the reference value
positive Boolean: true if the least double is desired; false otherwise
Return
Double the least or greater double value

parse

Added in API level 1
open fun parse(
    text: String,
    status: ParsePosition
): Number?

Parses a Number from the input text.

Parameters
source the String to parse
parsePosition the parse position
text String: the source text.
status ParsePosition: an input-output parameter. On input, the status.index field indicates the first character of the source text that should be parsed. On exit, if no error occurred, status.index is set to the first unparsed character in the source text. On exit, if an error did occur, status.index is unchanged and status.errorIndex is set to the first index of the character that caused the parse to fail.
Return
Number? A Number representing the value of the number parsed.
Exceptions
java.lang.NullPointerException if status is null or if text is null and the list of choice strings is not empty.

previousDouble

Added in API level 1
static fun previousDouble(d: Double): Double

Finds the greatest double less than d. If NaN, returns same value.

Parameters
d Double: the reference value
Return
Double the greatest double value less than d

See Also

    setChoices

    Added in API level 1
    open fun setChoices(
        limits: DoubleArray!,
        formats: Array<String!>!
    ): Unit

    Set the choices to be used in formatting.

    Parameters
    limits DoubleArray!: contains the top value that you want parsed with that format, and should be in ascending sorted order. When formatting X, the choice will be the i, where limit[i] ≤ X < limit[i+1]. If the limit array is not in ascending order, the results of formatting will be incorrect.
    formats Array<String!>!: are the formats you want to use for each limit. They can be either Format objects or Strings. When formatting with object Y, if the object is a NumberFormat, then ((NumberFormat) Y).format(X) is called. Otherwise Y.toString() is called.
    Exceptions
    java.lang.NullPointerException if limits or formats is null

    toPattern

    Added in API level 1
    open fun toPattern(): String!

    Gets the pattern.

    Return
    String! the pattern string