1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 1996-2016, Google, International Business Machines Corporation and 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 10 package com.ibm.icu.text; 11 12 import java.text.ParsePosition; 13 import java.util.Locale; 14 15 import com.ibm.icu.impl.number.DecimalFormatProperties; 16 import com.ibm.icu.util.CurrencyAmount; 17 import com.ibm.icu.util.ULocale; 18 19 /** 20 * The CompactDecimalFormat produces abbreviated numbers, suitable for display in environments will 21 * limited real estate. For example, 'Hits: 1.2B' instead of 'Hits: 1,200,000,000'. The format will 22 * be appropriate for the given language, such as "1,2 Mrd." for German. 23 * 24 * <p>For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345), the result will be 25 * short for supported languages. However, the result may sometimes exceed 7 characters, such as 26 * when there are combining marks or thin characters. In such cases, the visual width in fonts 27 * should still be short. 28 * 29 * <p>By default, there are 2 significant digits. After creation, if more than three significant 30 * digits are set (with setMaximumSignificantDigits), or if a fixed number of digits are set (with 31 * setMaximumIntegerDigits or setMaximumFractionDigits), then result may be wider. 32 * 33 * <p>The "short" style is also capable of formatting currency amounts, such as "$1.2M" instead of 34 * "$1,200,000.00" (English) or "5,3Mio." instead of "5.300.000,00 " (German). Localized data 35 * concerning longer formats is not available yet in the Unicode CLDR. Because of this, attempting 36 * to format a currency amount using the "long" style will produce an UnsupportedOperationException. 37 * 38 * <p>At this time, negative numbers and parsing are not supported, and will produce an 39 * UnsupportedOperationException. Resetting the pattern prefixes or suffixes is not supported; the 40 * method calls are ignored. 41 * 42 * <p>Note that important methods, like setting the number of decimals, will be moved up from 43 * DecimalFormat to NumberFormat. 44 * 45 * @author markdavis 46 * @stable ICU 49 47 */ 48 public class CompactDecimalFormat extends DecimalFormat { 49 50 private static final long serialVersionUID = 4716293295276629682L; 51 52 /** 53 * Style parameter for CompactDecimalFormat. 54 * 55 * @stable ICU 50 56 */ 57 public enum CompactStyle { 58 /** 59 * Short version, like "1.2T" 60 * 61 * @stable ICU 50 62 */ 63 SHORT, 64 /** 65 * Longer version, like "1.2 trillion", if available. May return same result as SHORT if not. 66 * 67 * @stable ICU 50 68 */ 69 LONG 70 } 71 72 /** 73 * Creates a CompactDecimalFormat appropriate for a locale. The result may be affected by the 74 * number system in the locale, such as ar-u-nu-latn. 75 * 76 * @param locale the desired locale 77 * @param style the compact style 78 * @stable ICU 50 79 */ 80 public static CompactDecimalFormat getInstance(ULocale locale, CompactStyle style) { 81 return new CompactDecimalFormat(locale, style); 82 } 83 84 /** 85 * Creates a CompactDecimalFormat appropriate for a locale. The result may be affected by the 86 * number system in the locale, such as ar-u-nu-latn. 87 * 88 * @param locale the desired locale 89 * @param style the compact style 90 * @stable ICU 50 91 */ 92 public static CompactDecimalFormat getInstance(Locale locale, CompactStyle style) { 93 return new CompactDecimalFormat(ULocale.forLocale(locale), style); 94 } 95 96 /** 97 * The public mechanism is CompactDecimalFormat.getInstance(). 98 * 99 * @param locale the desired locale 100 * @param style the compact style 101 */ 102 CompactDecimalFormat(ULocale locale, CompactStyle style) { 103 // Minimal properties: let the non-shim code path do most of the logic for us. 104 symbols = DecimalFormatSymbols.getInstance(locale); 105 properties = new DecimalFormatProperties(); 106 properties.setCompactStyle(style); 107 properties.setGroupingSize(-2); // do not forward grouping information 108 properties.setMinimumGroupingDigits(2); 109 exportedProperties = new DecimalFormatProperties(); 110 refreshFormatter(); 111 } 112 113 /** 114 * Parsing is currently unsupported, and throws an UnsupportedOperationException. 115 * 116 * @stable ICU 49 117 */ 118 @Override 119 public Number parse(String text, ParsePosition parsePosition) { 120 throw new UnsupportedOperationException(); 121 } 122 123 /** 124 * Parsing is currently unsupported, and throws an UnsupportedOperationException. 125 * 126 * @stable ICU 49 127 */ 128 @Override 129 public CurrencyAmount parseCurrency(CharSequence text, ParsePosition parsePosition) { 130 throw new UnsupportedOperationException(); 131 } 132 } 133