Class CompactDecimalFormat
- All Implemented Interfaces:
Serializable, Cloneable
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. €" instead of "5.300.000,00 €" (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.
- Author:
- markdavis
- See Also:
- Status:
- Stable ICU 49.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumStyle parameter for CompactDecimalFormat.Nested classes/interfaces inherited from class DecimalFormat
DecimalFormat.PropertySetterModifier and TypeClassDescriptionstatic interfaceDeprecated.This API is ICU internal only.Nested classes/interfaces inherited from class NumberFormat
NumberFormat.Field, NumberFormat.NumberFormatFactory, NumberFormat.SimpleNumberFormatFactoryModifier and TypeClassDescriptionstatic classThe instances of this inner class are used as attribute keys and values in AttributedCharacterIterator that NumberFormat.formatToCharacterIterator() method returns.static classA NumberFormatFactory is used to register new number formats.static classA NumberFormatFactory that supports a single locale.Nested classes/interfaces inherited from class UFormat
UFormat.SpanFieldModifier and TypeClassDescriptionstatic classA field that represents a span of text that may be composed with other fields. -
Field Summary
Fields inherited from class DecimalFormat
MINIMUM_GROUPING_DIGITS_AUTO, MINIMUM_GROUPING_DIGITS_MIN2, PAD_AFTER_PREFIX, PAD_AFTER_SUFFIX, PAD_BEFORE_PREFIX, PAD_BEFORE_SUFFIXModifier and TypeFieldDescriptionstatic final int[icu] Constant forDecimalFormat.setMinimumGroupingDigits(int)to specify display grouping using the default strategy for all locales.static final int[icu] Constant forDecimalFormat.setMinimumGroupingDigits(int)to specify display grouping using locale defaults, except do not show grouping on values smaller than 10000 (such that there is a minimum of two digits before the first separator).static final int[icu] Constant forDecimalFormat.getPadPosition()andDecimalFormat.setPadPosition(int)to specify pad characters inserted after the prefix.static final int[icu] Constant forDecimalFormat.getPadPosition()andDecimalFormat.setPadPosition(int)to specify pad characters inserted after the suffix.static final int[icu] Constant forDecimalFormat.getPadPosition()andDecimalFormat.setPadPosition(int)to specify pad characters inserted before the prefix.static final int[icu] Constant forDecimalFormat.getPadPosition()andDecimalFormat.setPadPosition(int)to specify pad characters inserted before the suffix.Fields inherited from class NumberFormat
ACCOUNTINGCURRENCYSTYLE, CASHCURRENCYSTYLE, CURRENCYSTYLE, FRACTION_FIELD, INTEGER_FIELD, INTEGERSTYLE, ISOCURRENCYSTYLE, NUMBERSTYLE, PERCENTSTYLE, PLURALCURRENCYSTYLE, SCIENTIFICSTYLE, STANDARDCURRENCYSTYLEModifier and TypeFieldDescriptionstatic final int[icu] Constant to specify currency style of format which uses currency symbol to represent currency for accounting, for example: "($3.00), instead of "-$3.00" (NumberFormat.CURRENCYSTYLE).static final int[icu] Constant to specify currency cash style of format which uses currency ISO code to represent currency, for example: "NT$3" instead of "NT$3.23".static final int[icu] Constant to specify general currency style of format.static final intField constant used to construct a FieldPosition object.static final intField constant used to construct a FieldPosition object.static final int[icu] Constant to specify a integer number style format.static final int[icu] Constant to specify currency style of format which uses currency ISO code to represent currency, for example: "USD3.00".static final int[icu] Constant to specify normal number style of format.static final int[icu] Constant to specify a style of format to display percent.static final int[icu] Constant to specify currency style of format which uses currency long name with plural format to represent currency, for example, "3.00 US Dollars".static final int[icu] Constant to specify a style of format to display scientific number.static final int[icu] Constant to specify currency style of format which uses currency symbol to represent currency, for example "$3.00", using non-accounting style for negative values (e.g. minus sign). -
Method Summary
Modifier and TypeMethodDescriptionstatic CompactDecimalFormatgetInstance(ULocale locale, CompactDecimalFormat.CompactStyle style) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static CompactDecimalFormatgetInstance(Locale locale, CompactDecimalFormat.CompactStyle style) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.parse(String text, ParsePosition parsePosition) Parsing is currently unsupported, and throws an UnsupportedOperationException.parseCurrency(CharSequence text, ParsePosition parsePosition) Parsing is currently unsupported, and throws an UnsupportedOperationException.Methods inherited from class DecimalFormat
applyLocalizedPattern, applyPattern, areSignificantDigitsUsed, clone, equals, format, format, format, format, format, format, formatToCharacterIterator, getCurrency, getCurrencyPluralInfo, getCurrencyUsage, getDecimalFormatSymbols, getFixedDecimal, getFormatWidth, getGroupingSize, getMathContext, getMathContextICU, getMaximumFractionDigits, getMaximumIntegerDigits, getMaximumSignificantDigits, getMinimumExponentDigits, getMinimumFractionDigits, getMinimumGroupingDigits, getMinimumIntegerDigits, getMinimumSignificantDigits, getMultiplier, getNegativePrefix, getNegativeSuffix, getPadCharacter, getPadPosition, getParseMaxDigits, getPositivePrefix, getPositiveSuffix, getRoundingIncrement, getRoundingMode, getSecondaryGroupingSize, hashCode, isDecimalPatternMatchRequired, isDecimalSeparatorAlwaysShown, isExponentSignAlwaysShown, isGroupingUsed, isParseBigDecimal, isParseCaseSensitive, isParseIntegerOnly, isParseNoExponent, isParseStrict, isScientificNotation, isSignAlwaysShown, setCurrency, setCurrencyPluralInfo, setCurrencyUsage, setDecimalFormatSymbols, setDecimalPatternMatchRequired, setDecimalSeparatorAlwaysShown, setExponentSignAlwaysShown, setFormatWidth, setGroupingSize, setGroupingUsed, setMathContext, setMathContextICU, setMaximumFractionDigits, setMaximumIntegerDigits, setMaximumSignificantDigits, setMinimumExponentDigits, setMinimumFractionDigits, setMinimumGroupingDigits, setMinimumIntegerDigits, setMinimumSignificantDigits, setMultiplier, setNegativePrefix, setNegativeSuffix, setPadCharacter, setPadPosition, setParseBigDecimal, setParseCaseSensitive, setParseIntegerOnly, setParseMaxDigits, setParseNoExponent, setParseStrict, setParseStrictMode, setPositivePrefix, setPositiveSuffix, setProperties, setRoundingIncrement, setRoundingIncrement, setRoundingIncrement, setRoundingMode, setScientificNotation, setSecondaryGroupingSize, setSignAlwaysShown, setSignificantDigitsUsed, toLocalizedPattern, toNumberFormatter, toPattern, toStringModifier and TypeMethodDescriptionvoidapplyLocalizedPattern(String localizedPattern) Converts the given string to standard notation and then parses it usingDecimalFormat.applyPattern(String).voidapplyPattern(String pattern) Parses the given pattern string and overwrites the settings specified in the pattern string.boolean[icu] Returns whether significant digits are being used in rounding.clone()Overrides clone.booleanTests for equality between this formatter and another formatter.format(double number, StringBuffer result, FieldPosition fieldPosition) Specialization of format.format(long number, StringBuffer result, FieldPosition fieldPosition) Specialization of format.format(BigDecimal number, StringBuffer result, FieldPosition fieldPosition) [icu] Formats an ICU BigDecimal.format(CurrencyAmount currAmt, StringBuffer result, FieldPosition fieldPosition) [icu] Formats a CurrencyAmount.format(BigDecimal number, StringBuffer result, FieldPosition fieldPosition) [icu] Formats a BigDecimal.format(BigInteger number, StringBuffer result, FieldPosition fieldPosition) [icu] Formats a BigInteger.Returns the currency used to display currency amounts.[icu] Returns the current instance of CurrencyPluralInfo.[icu] Returns the strategy for rounding currency amounts.Returns a copy of the decimal format symbols used by this formatter.getFixedDecimal(double number) Deprecated.This API is ICU internal only.intReturns the minimum number of characters in formatted output.intReturns the primary grouping size in use.[icu] Returns theMathContextbeing used to round numbers.[icu] Returns theMathContextbeing used to round numbers.intReturns the effective maximum number of integer digits after the decimal separator.intReturns the effective maximum number of digits before the decimal separator.int[icu] Returns the effective maximum number of significant digits displayed.byte[icu] Returns the minimum number of digits printed in the exponent in scientific notation.intReturns the effective minimum number of integer digits after the decimal separator.int[icu] Returns the minimum number of digits before grouping is triggered.intReturns the effective minimum number of digits before the decimal separator.int[icu] Returns the effective minimum number of significant digits displayed.intReturns the multiplier being applied to numbers before they are formatted.Affixes: Gets the negative prefix string currently being used to format numbers.Affixes: Gets the negative suffix string currently being used to format numbers.char[icu] Returns the character used for padding.int[icu] Returns the position used for padding.intDeprecated.ICU 59 Setting max parse digits has no effect since ICU4J 59.Affixes: Gets the positive prefix string currently being used to format numbers.Affixes: Gets the positive suffix string currently being used to format numbers.[icu] Returns the increment to which numbers are being rounded.intReturns the rounding mode being used to round numbers.int[icu] Returns the secondary grouping size in use.inthashCode()boolean[icu] Returns whether the presence of a decimal point must match the pattern.booleanReturns whether the decimal separator is shown on integers.boolean[icu] Returns whether the sign (plus or minus) is always printed in scientific notation.booleanReturns whether or not grouping separators are being printed in the output.booleanReturns whetherDecimalFormat.parse(String, ParsePosition)will always return a BigDecimal.boolean[icu] Returns whether to force case (uppercase/lowercase) to match when parsing.booleanReturns true if this format will parse numbers as integers only.boolean[icu] Returns whether to ignore exponents when parsing.boolean[icu] Returns whether strict parsing is in effect.boolean[icu] Returns whether scientific (exponential) notation is enabled on this formatter.boolean[icu] Returns whether the sign is being shown on positive numbers.voidsetCurrency(Currency currency) Sets the currency to be used when formatting numbers.voidsetCurrencyPluralInfo(CurrencyPluralInfo newInfo) [icu] Sets a custom instance of CurrencyPluralInfo.void[icu] Sets the currency-dependent strategy to use when rounding numbers.voidsetDecimalFormatSymbols(DecimalFormatSymbols newSymbols) Sets the decimal format symbols used by this formatter.voidsetDecimalPatternMatchRequired(boolean value) [icu] Parsing: This method is used to either require or forbid the presence of a decimal point in the string being parsed (disabled by default).voidsetDecimalSeparatorAlwaysShown(boolean value) Separators: Sets whether the decimal separator (a period in en-US) is shown on integers.voidsetExponentSignAlwaysShown(boolean expSignAlways) [icu] Scientific Notation: Sets whether the sign (plus or minus) is always to be shown in the exponent in scientific notation.voidsetFormatWidth(int width) Padding: Sets the minimum width of the string output by the formatting pipeline.voidsetGroupingSize(int width) Grouping: Sets the primary grouping size (distance between grouping separators) used when formatting large numbers.voidsetGroupingUsed(boolean enabled) Grouping: Sets whether grouping is to be used when formatting numbers.voidsetMathContext(MathContext mathContext) [icu] Rounding and Digit Limits: Sets theMathContextused to round numbers.voidsetMathContextICU(MathContext mathContextICU) [icu] Rounding and Digit Limits: Overload ofDecimalFormat.setMathContext(MathContext)forMathContext.voidsetMaximumFractionDigits(int value) Rounding and Digit Limits: Sets the maximum number of digits to display after the decimal separator.voidsetMaximumIntegerDigits(int value) Rounding and Digit Limits: Sets the maximum number of digits to display before the decimal separator.voidsetMaximumSignificantDigits(int value) [icu] Rounding and Digit Limits: Sets the maximum number of significant digits to be displayed.voidsetMinimumExponentDigits(byte minExpDig) [icu] Scientific Notation: Sets the minimum number of digits to be printed in the exponent.voidsetMinimumFractionDigits(int value) Rounding and Digit Limits: Sets the minimum number of digits to display after the decimal separator.voidsetMinimumGroupingDigits(int number) [icu] Sets the minimum number of digits that must be before the first grouping separator in order for the grouping separator to be printed.voidsetMinimumIntegerDigits(int value) Rounding and Digit Limits: Sets the minimum number of digits to display before the decimal separator.voidsetMinimumSignificantDigits(int value) [icu] Rounding and Digit Limits: Sets the minimum number of significant digits to be displayed.voidsetMultiplier(int multiplier) Sets a number that will be used to multiply all numbers prior to formatting.voidsetNegativePrefix(String prefix) Affixes: Sets the string to prepend to negative numbers.voidsetNegativeSuffix(String suffix) Affixes: Sets the string to append to negative numbers.voidsetPadCharacter(char padChar) [icu] Padding: Sets the character used to pad numbers that are narrower than the width specified inDecimalFormat.setFormatWidth(int).voidsetPadPosition(int padPos) [icu] Padding: Sets the position where to insert the pad character when narrower than the width specified inDecimalFormat.setFormatWidth(int).voidsetParseBigDecimal(boolean value) Whether to makeDecimalFormat.parse(String, ParsePosition)prefer returning aBigDecimalwhen possible.voidsetParseCaseSensitive(boolean value) [icu] Specifies whether parsing should require cases to match in affixes, exponent separators, and currency codes.voidsetParseIntegerOnly(boolean parseIntegerOnly) Parsing: Sets whether to ignore the fraction part of a number when parsing (defaults to false).voidsetParseMaxDigits(int maxDigits) Deprecated.ICU 59 Setting max parse digits has no effect since ICU4J 59.voidsetParseNoExponent(boolean value) [icu] Specifies whether to stop parsing when an exponent separator is encountered.voidsetParseStrict(boolean parseStrict) [icu] Sets whether strict parsing is in effect.voidsetParseStrictMode(com.ibm.icu.impl.number.DecimalFormatProperties.ParseMode parseMode) Deprecated.This API is ICU internal only.voidsetPositivePrefix(String prefix) Affixes: Sets the string to prepend to positive numbers.voidsetPositiveSuffix(String suffix) Affixes: Sets the string to append to positive numbers.voidDeprecated.This API is ICU internal only.voidsetRoundingIncrement(double increment) [icu] Rounding and Digit Limits: Overload ofDecimalFormat.setRoundingIncrement(java.math.BigDecimal).voidsetRoundingIncrement(BigDecimal increment) [icu] Rounding and Digit Limits: Overload ofDecimalFormat.setRoundingIncrement(java.math.BigDecimal).voidsetRoundingIncrement(BigDecimal increment) [icu] Rounding and Digit Limits: Sets an increment, or interval, to which numbers are rounded.voidsetRoundingMode(int roundingMode) Rounding and Digit Limits: Sets theRoundingModeused to round numbers.voidsetScientificNotation(boolean useScientific) [icu] Scientific Notation: Sets whether this formatter should print in scientific (exponential) notation.voidsetSecondaryGroupingSize(int width) [icu] Grouping: Sets the secondary grouping size (distance between grouping separators after the first separator) used when formatting large numbers.voidsetSignAlwaysShown(boolean value) Sets whether to always shown the plus sign ('+' in en) on positive numbers.voidsetSignificantDigitsUsed(boolean useSignificantDigits) [icu] Rounding and Digit Limits: Sets whether significant digits are to be used in rounding.CallsDecimalFormat.toPattern()and converts the string to localized notation.Converts this DecimalFormat to a NumberFormatter.Serializes this formatter object to a decimal format pattern string.toString()Returns the default value of toString() with extra DecimalFormat-specific information appended to the end of the string.Methods inherited from class NumberFormat
format, format, format, format, format, format, format, getAvailableLocales, getAvailableULocales, getContext, getCurrencyInstance, getCurrencyInstance, getCurrencyInstance, getEffectiveCurrency, getInstance, getInstance, getInstance, getInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getIntegerInstance, getNumberInstance, getNumberInstance, getNumberInstance, getPattern, getPattern, getPatternForStyle, getPatternForStyleAndNumberingSystem, getPercentInstance, getPercentInstance, getPercentInstance, getScientificInstance, getScientificInstance, getScientificInstance, parse, parseObject, registerFactory, setContext, unregisterModifier and TypeMethodDescriptionfinal Stringformat(double number) Specialization of format.final Stringformat(long number) Specialization of format.final Stringformat(BigDecimal number) [icu] Convenience method to format an ICU BigDecimal.final Stringformat(CurrencyAmount currAmt) [icu] Convenience method to format a CurrencyAmount.format(Object number, StringBuffer toAppendTo, FieldPosition pos) Formats a number and appends the resulting text to the given string buffer.final Stringformat(BigDecimal number) Convenience method to format a BigDecimal.final Stringformat(BigInteger number) [icu] Convenience method to format a BigInteger.static Locale[]Returns the list of Locales for which NumberFormats are available.static ULocale[][icu] Returns the list of Locales for which NumberFormats are available.[icu] Get the formatter's DisplayContext value for the specified DisplayContext.Type, such as CAPITALIZATION.static final NumberFormatNOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetCurrencyInstance(ULocale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetCurrencyInstance(Locale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.protected CurrencyDeprecated.This API is ICU internal only.static final NumberFormatNOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static final NumberFormatgetInstance(int style) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetInstance(ULocale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetInstance(ULocale desiredLocale, int choice) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetInstance(Locale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetInstance(Locale inLocale, int style) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static final NumberFormatNOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetIntegerInstance(ULocale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetIntegerInstance(Locale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static final NumberFormatNOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetNumberInstance(ULocale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetNumberInstance(Locale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.protected static StringgetPattern(ULocale forLocale, int choice) Returns the pattern for the provided locale and choice.protected static StringgetPattern(Locale forLocale, int choice) Deprecated.ICU 3.4 subclassers should override getPattern(ULocale, int) instead of this method.static StringgetPatternForStyle(ULocale forLocale, int choice) Deprecated.This API is ICU internal only.static StringgetPatternForStyleAndNumberingSystem(ULocale forLocale, String nsName, int choice) Deprecated.This API is ICU internal only.static final NumberFormatNOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetPercentInstance(ULocale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetPercentInstance(Locale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static final NumberFormatNOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetScientificInstance(ULocale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.static NumberFormatgetScientificInstance(Locale inLocale) NOTE: New users are strongly encouraged to useNumberFormatterinstead of NumberFormat.Parses text from the beginning of the given string to produce a number.final ObjectparseObject(String source, ParsePosition parsePosition) Parses text from a string to produce a number.static Object[icu] Registers a new NumberFormatFactory.voidsetContext(DisplayContext context) [icu] Set a particular DisplayContext value in the formatter, such as CAPITALIZATION_FOR_STANDALONE.static booleanunregister(Object registryKey) [icu] Unregisters the factory or instance associated with this key (obtained from registerInstance or registerFactory).Methods inherited from class UFormat
getLocaleModifier and TypeMethodDescriptionfinal ULocalegetLocale(ULocale.Type type) Return the locale that was used to create this object, or null.Methods inherited from class Format
format, parseObject
-
Method Details
-
getInstance
public static CompactDecimalFormat getInstance(ULocale locale, CompactDecimalFormat.CompactStyle style) NOTE: New users are strongly encouraged to useNumberFormatterinstead 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- the desired localestyle- the compact style- Status:
- Stable ICU 50.
-
getInstance
public static CompactDecimalFormat getInstance(Locale locale, CompactDecimalFormat.CompactStyle style) NOTE: New users are strongly encouraged to useNumberFormatterinstead 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- the desired localestyle- the compact style- Status:
- Stable ICU 50.
-
parse
Parsing is currently unsupported, and throws an UnsupportedOperationException.- Overrides:
parsein classDecimalFormat- See Also:
- Status:
- Stable ICU 49.
-
parseCurrency
Parsing is currently unsupported, and throws an UnsupportedOperationException.- Overrides:
parseCurrencyin classDecimalFormat- Parameters:
text- the text to parseparsePosition- 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:
- a CurrencyAmount, or null upon failure
- Status:
- Stable ICU 49.
-