CompactDecimalFormat


public class CompactDecimalFormat
extends DecimalFormat

java.lang.Object
   ↳ java.text.Format
     ↳ android.icu.text.UFormat
       ↳ android.icu.text.NumberFormat
         ↳ android.icu.text.DecimalFormat
           ↳ android.icu.text.CompactDecimalFormat


Formats numbers in compact (abbreviated) notation, like "1.2K" instead of "1200".

IMPORTANT: New users are strongly encouraged to see if NumberFormatter fits their use case. Although not deprecated, this class, CompactDecimalFormat, is provided for backwards compatibility only.


The CompactDecimalFormat produces abbreviated numbers, suitable for display in environments will limited real estate. For example, 'Hits: 1.2B' instead of 'Hits: 1,200,000,000'. The format will be appropriate for the given language, such as "1,2 Mrd." for German.

For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345), the result will be short for supported languages. However, the result may sometimes exceed 7 characters, such as when there are combining marks or thin characters. In such cases, the visual width in fonts should still be short.

By default, there are 2 significant digits. After creation, if more than three significant digits are set (with setMaximumSignificantDigits), or if a fixed number of digits are set (with setMaximumIntegerDigits or setMaximumFractionDigits), then result may be wider.

The "short" style is also capable of formatting currency amounts, such as "$1.2M" instead of "$1,200,000.00" (English) or "5,3 Mio. \u20ac" instead of "5.300.000,00 \u20ac" (German). Localized data concerning longer formats is not available yet in the Unicode CLDR. Because of this, attempting to format a currency amount using the "long" style will produce an UnsupportedOperationException.

At this time, negative numbers and parsing are not supported, and will produce an UnsupportedOperationException. Resetting the pattern prefixes or suffixes is not supported; the method calls are ignored.

Note that important methods, like setting the number of decimals, will be moved up from DecimalFormat to NumberFormat.

Summary

Inherited constants

Public methods

static CompactDecimalFormat getInstance(ULocale locale, CompactDecimalFormat.CompactStyle style)

NOTE: New users are strongly encouraged to use NumberFormatter instead of NumberFormat.

static CompactDecimalFormat getInstance(Locale locale, CompactDecimalFormat.CompactStyle style)

NOTE: New users are strongly encouraged to use NumberFormatter instead of NumberFormat.

Number parse(String text, ParsePosition parsePosition)

Parsing is currently unsupported, and throws an UnsupportedOperationException.

CurrencyAmount parseCurrency(CharSequence text, ParsePosition parsePosition)

Parsing is currently unsupported, and throws an UnsupportedOperationException.

Inherited methods

Public methods

getInstance

Added in API level 24
public static CompactDecimalFormat getInstance (ULocale locale, 
                CompactDecimalFormat.CompactStyle style)

NOTE: New users are strongly encouraged to use NumberFormatter instead of NumberFormat.


Creates a CompactDecimalFormat appropriate for a locale. The result may be affected by the number system in the locale, such as ar-u-nu-latn.

Parameters
locale ULocale: the desired locale

style CompactDecimalFormat.CompactStyle: the compact style

Returns
CompactDecimalFormat

getInstance

Added in API level 24
public static CompactDecimalFormat getInstance (Locale locale, 
                CompactDecimalFormat.CompactStyle style)

NOTE: New users are strongly encouraged to use NumberFormatter instead of NumberFormat.


Creates a CompactDecimalFormat appropriate for a locale. The result may be affected by the number system in the locale, such as ar-u-nu-latn.

Parameters
locale Locale: the desired locale

style CompactDecimalFormat.CompactStyle: the compact style

Returns
CompactDecimalFormat

parse

Added in API level 24
public Number parse (String text, 
                ParsePosition parsePosition)

Parsing is currently unsupported, and throws an UnsupportedOperationException.

Parameters
text String

parsePosition ParsePosition

Returns
Number

parseCurrency

Added in API level 24
public CurrencyAmount parseCurrency (CharSequence text, 
                ParsePosition parsePosition)

Parsing is currently unsupported, and throws an UnsupportedOperationException.

Parameters
text CharSequence: the text to parse

parsePosition ParsePosition: input-output position; on input, the position within text to match; must have 0 <= pos.getIndex() < text.length(); on output, the position after the last matched character. If the parse fails, the position in unchanged upon output.

Returns
CurrencyAmount a CurrencyAmount, or null upon failure