Added in API level 1

NumberFormat

abstract class NumberFormat : Format
kotlin.Any
   ↳ java.text.Format
   ↳ java.text.NumberFormat

NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales have number formats, and what their names are.

NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

To format a number for the current Locale, use one of the factory class methods:

<code>myString = NumberFormat.getInstance().format(myNumber);
  </code>
If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.
<code>NumberFormat nf = NumberFormat.getInstance();
  for (int i = 0; i &lt; myNumber.length; ++i) {
      output.println(nf.format(myNumber[i]) + "; ");
  }
  </code>
To format a number for a different Locale, specify it in the call to getInstance.
<code>NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
  </code>

If the locale contains "nu" (numbers) Unicode extensions, the decimal digits, and/or the country used for formatting are overridden.

You can also use a NumberFormat to parse numbers:

<code>myNumber = nf.parse(myString);
  </code>
Use getInstance or getNumberInstance to get the normal number format. Use getIntegerInstance to get an integer number format. Use getCurrencyInstance to get the currency number format. Use getCompactNumberInstance to get the compact number format to format a number in shorter form. For example, 2000 can be formatted as "2K" in US locale. Use getPercentInstance to get a format for displaying percentages. With this format, a fraction like 0.53 is displayed as 53%.

You can also control the display of numbers with such methods as setMinimumFractionDigits. If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalFormat or CompactNumberFormat depending on the factory method used. This will work for the vast majority of locales; just remember to put it in a try block in case you encounter an unusual one.

NumberFormat and DecimalFormat are designed such that some controls work for formatting and others work for parsing. The following is the detailed description for each these control methods,

setParseIntegerOnly : only affects parsing, e.g. if true, "3456.78" → 3456 (and leaves the parse position just after index 6) if false, "3456.78" → 3456.78 (and leaves the parse position just after index 8) This is independent of formatting. If you want to not show a decimal point where there might be no digits after the decimal point, use setDecimalSeparatorAlwaysShown.

setDecimalSeparatorAlwaysShown : only affects formatting, and only where there might be no digits after the decimal point, such as with a pattern like "#,##0.##", e.g., if true, 3456.00 → "3,456." if false, 3456.00 → "3456" This is independent of parsing. If you want parsing to stop at the decimal point, use setParseIntegerOnly.

You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to:

  • progressively parse through pieces of a string
  • align the decimal point and other areas
For example, you can align numbers in two ways:
  1. If you are using a monospaced font with spacing for alignment, you can pass the FieldPosition in your format call, with field = INTEGER_FIELD. On output, getEndIndex will be set to the offset between the last character of the integer and the decimal. Add (desiredSpaceCount - getEndIndex) spaces at the front of the string.
  2. If you are using proportional fonts, instead of padding with spaces, measure the width of the string in pixels from the start to getEndIndex. Then move the pen by (desiredPixelWidth - widthToAlignmentPoint) before drawing the text. It also works where there is no decimal, but possibly additional characters at the end, e.g., with parentheses in negative numbers: "(12)" for -12.

Synchronization

Number formats are generally 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

Nested classes
open

Defines constants that are used as attribute keys in the AttributedCharacterIterator returned from NumberFormat.formatToCharacterIterator and as field identifiers in FieldPosition.

Constants
static Int

Field constant used to construct a FieldPosition object.

static Int

Field constant used to construct a FieldPosition object.

Protected constructors

Sole constructor.

Public methods
open Any

Overrides Cloneable.

open Boolean
equals(other: Any?)

Overrides equals.

String
format(number: Double)

Specialization of format.

abstract StringBuffer
format(number: Double, toAppendTo: StringBuffer, pos: FieldPosition)

Specialization of format.

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

Formats a number and appends the resulting text to the given string buffer.

String
format(number: Long)

Specialization of format.

abstract StringBuffer
format(number: Long, toAppendTo: StringBuffer, pos: FieldPosition)

Specialization of format.

open static Array<Locale!>

Returns a scientific format for the current default locale.

open Currency?

Gets the currency used by this number format when formatting currency values.

static NumberFormat

Returns a currency format for the current default FORMAT locale.

open static NumberFormat

Returns a currency format for the specified locale.

static NumberFormat

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

open static NumberFormat
getInstance(inLocale: Locale)

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

static NumberFormat

Returns an integer number format for the current default FORMAT locale.

open static NumberFormat

Returns an integer number format for the specified locale.

open Int

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

open Int

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

open Int

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

open Int

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

static NumberFormat

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

open static NumberFormat

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

static NumberFormat

Returns a percentage format for the current default FORMAT locale.

open static NumberFormat

Returns a percentage format for the specified locale.

open RoundingMode

Gets the java.math.RoundingMode used in this NumberFormat.

open Int

Overrides hashCode.

open Boolean

Returns true if grouping is used in this format.

open Boolean

Returns true if this format will parse numbers as integers only.

open Number?
parse(source: String)

Parses text from the beginning of the given string to produce a number.

abstract Number?
parse(source: String, parsePosition: ParsePosition)

Returns a Long if possible (e.g., within the range [Long.MIN_VALUE, Long.MAX_VALUE] and with no decimals), otherwise a Double.

Any?

Parses text from a string to produce a Number.

open Unit
setCurrency(currency: Currency)

Sets the currency used by this number format when formatting currency values.

open Unit

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

open Unit

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

open Unit

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

open Unit

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

open Unit

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

open Unit

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

open Unit
setRoundingMode(roundingMode: RoundingMode?)

Sets the java.math.RoundingMode used in this NumberFormat.

Inherited functions

Constants

FRACTION_FIELD

Added in API level 1
static val FRACTION_FIELD: Int

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

Value: 1

INTEGER_FIELD

Added in API level 1
static val INTEGER_FIELD: Int

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

Value: 0

Protected constructors

NumberFormat

Added in API level 1
protected NumberFormat()

Sole constructor. (For invocation by subclass constructors, typically implicit.)

Public methods

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

Overrides equals.

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
fun format(number: Double): String

Specialization of format.

Parameters
number Double: the double number to format
Return
String the formatted String
Exceptions
java.lang.ArithmeticException if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY

See Also

    format

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

    Specialization of format.

    Parameters
    number Double: the double number to format
    toAppendTo StringBuffer: the StringBuffer to which the formatted text is to be appended
    pos FieldPosition: 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.
    Return
    StringBuffer the formatted StringBuffer
    Exceptions
    java.lang.ArithmeticException if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY

    See Also

      format

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

      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.

      Parameters
      obj The object to format
      toAppendTo StringBuffer: the StringBuffer to which the formatted text is to be appended
      pos FieldPosition: 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.
      number Any: the number to format
      Return
      StringBuffer the value passed in as toAppendTo
      Exceptions
      java.lang.NullPointerException if toAppendTo or pos is null
      java.lang.IllegalArgumentException if number is null or not an instance of Number.
      java.lang.ArithmeticException if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY

      format

      Added in API level 1
      fun format(number: Long): String

      Specialization of format.

      Parameters
      number Long: the long number to format
      Return
      String the formatted String
      Exceptions
      java.lang.ArithmeticException if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY

      See Also

        format

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

        Specialization of format.

        Parameters
        number Long: the long number to format
        toAppendTo StringBuffer: the StringBuffer to which the formatted text is to be appended
        pos FieldPosition: 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

        See Also

          getAvailableLocales

          Added in API level 1
          open static fun getAvailableLocales(): Array<Locale!>

          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.

          Parameters
          inLocale the desired locale /*public* static NumberFormat getScientificInstance(Locale inLocale) { return getInstance(inLocale, null, SCIENTIFICSTYLE); }

          getCurrency

          Added in API level 1
          open fun getCurrency(): Currency?

          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.

          Return
          Currency? the currency used by this number format, or null
          Exceptions
          java.lang.UnsupportedOperationException if the number format class doesn't implement currency formatting

          getCurrencyInstance

          Added in API level 1
          static fun getCurrencyInstance(): NumberFormat

          Returns a currency format for the current default FORMAT locale.

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

          Return
          NumberFormat the NumberFormat instance for currency formatting

          getCurrencyInstance

          Added in API level 1
          open static fun getCurrencyInstance(inLocale: Locale): NumberFormat

          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.

          Parameters
          inLocale Locale: the desired locale
          Return
          NumberFormat the NumberFormat instance for currency formatting

          getInstance

          Added in API level 1
          static fun getInstance(): NumberFormat

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

          Return
          NumberFormat the NumberFormat instance for general-purpose number formatting

          getInstance

          Added in API level 1
          open static fun getInstance(inLocale: Locale): NumberFormat

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

          Parameters
          inLocale Locale: the desired locale
          Return
          NumberFormat the NumberFormat instance for general-purpose number formatting

          getIntegerInstance

          Added in API level 1
          static fun getIntegerInstance(): NumberFormat

          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)).

          Return
          NumberFormat a number format for integer values

          getIntegerInstance

          Added in API level 1
          open static fun getIntegerInstance(inLocale: Locale): NumberFormat

          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).

          Parameters
          inLocale Locale: the desired locale
          Return
          NumberFormat a number format for integer values

          getMaximumFractionDigits

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

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

          Return
          Int the maximum number of digits.

          getMaximumIntegerDigits

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

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

          Return
          Int the maximum number of digits

          getMinimumFractionDigits

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

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

          Return
          Int the minimum number of digits

          getMinimumIntegerDigits

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

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

          Return
          Int the minimum number of digits

          getNumberInstance

          Added in API level 1
          static fun getNumberInstance(): NumberFormat

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

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

          Return
          NumberFormat the NumberFormat instance for general-purpose number formatting

          getNumberInstance

          Added in API level 1
          open static fun getNumberInstance(inLocale: Locale): NumberFormat

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

          Parameters
          inLocale Locale: the desired locale
          Return
          NumberFormat the NumberFormat instance for general-purpose number formatting

          getPercentInstance

          Added in API level 1
          static fun getPercentInstance(): NumberFormat

          Returns a percentage format for the current default FORMAT locale.

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

          Return
          NumberFormat the NumberFormat instance for percentage formatting

          getPercentInstance

          Added in API level 1
          open static fun getPercentInstance(inLocale: Locale): NumberFormat

          Returns a percentage format for the specified locale.

          Parameters
          inLocale Locale: the desired locale
          Return
          NumberFormat the NumberFormat instance for percentage formatting

          getRoundingMode

          Added in API level 9
          open fun getRoundingMode(): RoundingMode

          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.

          Return
          RoundingMode The RoundingMode used for this NumberFormat.
          Exceptions
          java.lang.UnsupportedOperationException The default implementation always throws this exception

          hashCode

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

          Overrides hashCode.

          Return
          Int a hash code value for this object.

          isGroupingUsed

          Added in API level 1
          open fun isGroupingUsed(): Boolean

          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.

          Return
          Boolean true if grouping is used; false otherwise

          See Also

          isParseIntegerOnly

          Added in API level 1
          open fun isParseIntegerOnly(): Boolean

          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.

          Return
          Boolean true if numbers should be parsed as integers only; false otherwise

          parse

          Added in API level 1
          open fun parse(source: String): Number?

          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.

          Parameters
          source String: A String whose beginning should be parsed.
          Return
          Number? A Number parsed from the string.
          Exceptions
          java.text.ParseException if the beginning of the specified string cannot be parsed.

          parse

          Added in API level 1
          abstract fun parse(
              source: String,
              parsePosition: ParsePosition
          ): Number?

          Returns a Long if possible (e.g., within the range [Long.MIN_VALUE, Long.MAX_VALUE] and with no decimals), otherwise a Double. If IntegerOnly is set, will stop at a decimal point (or equivalent; e.g., for rational numbers "1 2/3", will stop after the 1). Does not throw an exception; if no object can be parsed, index is unchanged!

          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.

          Parameters
          source String: the String to parse
          parsePosition ParsePosition: the parse position
          Return
          Number? the parsed value

          parseObject

          Added in API level 1
          fun parseObject(
              source: String,
              pos: ParsePosition
          ): Any?

          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.

          Parameters
          source String: A String, part of which should be parsed.
          pos ParsePosition: A ParsePosition object with index and error index information as described above.
          Return
          Any? A Number parsed from the string. In case of error, returns null.
          Exceptions
          java.lang.NullPointerException if source or pos is null.

          setCurrency

          Added in API level 1
          open fun setCurrency(currency: Currency): Unit

          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.

          Parameters
          currency Currency: the new currency to be used by this number format
          Exceptions
          java.lang.UnsupportedOperationException if the number format class doesn't implement currency formatting
          java.lang.NullPointerException if currency is null

          setGroupingUsed

          Added in API level 1
          open fun setGroupingUsed(newValue: Boolean): Unit

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

          Parameters
          newValue Boolean: true if grouping is used; false otherwise

          See Also

          setMaximumFractionDigits

          Added in API level 1
          open fun setMaximumFractionDigits(newValue: Int): Unit

          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.

          Parameters
          newValue Int: the maximum number of fraction digits to be shown; if less than zero, then zero is used. The concrete subclass may enforce an upper limit to this value appropriate to the numeric type being formatted.

          setMaximumIntegerDigits

          Added in API level 1
          open fun setMaximumIntegerDigits(newValue: Int): Unit

          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.

          Parameters
          newValue Int: the maximum number of integer digits to be shown; if less than zero, then zero is used. The concrete subclass may enforce an upper limit to this value appropriate to the numeric type being formatted.

          setMinimumFractionDigits

          Added in API level 1
          open fun setMinimumFractionDigits(newValue: Int): Unit

          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

          Parameters
          newValue Int: the minimum number of fraction digits to be shown; if less than zero, then zero is used. The concrete subclass may enforce an upper limit to this value appropriate to the numeric type being formatted.

          setMinimumIntegerDigits

          Added in API level 1
          open fun setMinimumIntegerDigits(newValue: Int): Unit

          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

          Parameters
          newValue Int: the minimum number of integer digits to be shown; if less than zero, then zero is used. The concrete subclass may enforce an upper limit to this value appropriate to the numeric type being formatted.

          setParseIntegerOnly

          Added in API level 1
          open fun setParseIntegerOnly(value: Boolean): Unit

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

          Parameters
          value Boolean: true if numbers should be parsed as integers only; false otherwise

          setRoundingMode

          Added in API level 9
          open fun setRoundingMode(roundingMode: RoundingMode?): Unit

          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.

          Parameters
          roundingMode RoundingMode?: The RoundingMode to be used
          Exceptions
          java.lang.UnsupportedOperationException The default implementation always throws this exception
          java.lang.NullPointerException if roundingMode is null