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