37 */
38
39 package java.text;
40
41 import java.io.IOException;
42 import java.io.InvalidObjectException;
43 import java.io.ObjectInputStream;
44 import java.math.BigDecimal;
45 import java.math.BigInteger;
46 import java.math.RoundingMode;
47 import java.text.spi.NumberFormatProvider;
48 import java.util.ArrayList;
49 import java.util.Currency;
50 import java.util.Locale;
51 import java.util.ResourceBundle;
52 import java.util.concurrent.ConcurrentHashMap;
53 import java.util.concurrent.ConcurrentMap;
54 import java.util.concurrent.atomic.AtomicInteger;
55 import java.util.concurrent.atomic.AtomicLong;
56 import sun.util.locale.provider.LocaleProviderAdapter;
57
58 /**
59 * <code>DecimalFormat</code> is a concrete subclass of
60 * <code>NumberFormat</code> that formats decimal numbers. It has a variety of
61 * features designed to make it possible to parse and format numbers in any
62 * locale, including support for Western, Arabic, and Indic digits. It also
63 * supports different kinds of numbers, including integers (123), fixed-point
64 * numbers (123.4), scientific notation (1.23E4), percentages (12%), and
65 * currency amounts ($123). All of these can be localized.
66 *
67 * <p>To obtain a <code>NumberFormat</code> for a specific locale, including the
68 * default locale, call one of <code>NumberFormat</code>'s factory methods, such
69 * as <code>getInstance()</code>. In general, do not call the
70 * <code>DecimalFormat</code> constructors directly, since the
71 * <code>NumberFormat</code> factory methods may return subclasses other than
72 * <code>DecimalFormat</code>. If you need to customize the format object, do
73 * something like this:
74 *
75 * <blockquote><pre>
76 * NumberFormat f = NumberFormat.getInstance(loc);
377 * @author Alan Liu
378 */
379 public class DecimalFormat extends NumberFormat {
380
381 /**
382 * Creates a DecimalFormat using the default pattern and symbols
383 * for the default locale. This is a convenient way to obtain a
384 * DecimalFormat when internationalization is not the main concern.
385 * <p>
386 * To obtain standard formats for a given locale, use the factory methods
387 * on NumberFormat such as getNumberInstance. These factories will
388 * return the most appropriate sub-class of NumberFormat for a given
389 * locale.
390 *
391 * @see java.text.NumberFormat#getInstance
392 * @see java.text.NumberFormat#getNumberInstance
393 * @see java.text.NumberFormat#getCurrencyInstance
394 * @see java.text.NumberFormat#getPercentInstance
395 */
396 public DecimalFormat() {
397 Locale def = Locale.getDefault(Locale.Category.FORMAT);
398 // try to get the pattern from the cache
399 String pattern = cachedLocaleData.get(def);
400 if (pattern == null) { /* cache miss */
401 // Get the pattern for the default locale.
402 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class, def);
403 switch (adapter.getAdapterType()) {
404 case HOST:
405 case SPI:
406 adapter = LocaleProviderAdapter.getResourceBundleBased();
407 break;
408 }
409 ResourceBundle rb = adapter.getLocaleData().getNumberFormatData(def);
410 String[] all = rb.getStringArray("NumberPatterns");
411 pattern = all[0];
412 /* update cache */
413 cachedLocaleData.putIfAbsent(def, pattern);
414 }
415
416 // Always applyPattern after the symbols are set
417 this.symbols = DecimalFormatSymbols.getInstance(def);
418 applyPattern(pattern, false);
419 }
420
421
422 /**
423 * Creates a DecimalFormat using the given pattern and the symbols
424 * for the default locale. This is a convenient way to obtain a
425 * DecimalFormat when internationalization is not the main concern.
426 * <p>
427 * To obtain standard formats for a given locale, use the factory methods
428 * on NumberFormat such as getNumberInstance. These factories will
429 * return the most appropriate sub-class of NumberFormat for a given
430 * locale.
431 *
432 * @param pattern A non-localized pattern string.
433 * @exception NullPointerException if <code>pattern</code> is null
434 * @exception IllegalArgumentException if the given pattern is invalid.
435 * @see java.text.NumberFormat#getInstance
436 * @see java.text.NumberFormat#getNumberInstance
437 * @see java.text.NumberFormat#getCurrencyInstance
438 * @see java.text.NumberFormat#getPercentInstance
4137 * replaced with the monetary decimal separator.
4138 *
4139 * The CURRENCY_SIGN is not localized.
4140 */
4141 private static final char CURRENCY_SIGN = '\u00A4';
4142
4143 private static final char QUOTE = '\'';
4144
4145 private static FieldPosition[] EmptyFieldPositionArray = new FieldPosition[0];
4146
4147 // Upper limit on integer and fraction digits for a Java double
4148 static final int DOUBLE_INTEGER_DIGITS = 309;
4149 static final int DOUBLE_FRACTION_DIGITS = 340;
4150
4151 // Upper limit on integer and fraction digits for BigDecimal and BigInteger
4152 static final int MAXIMUM_INTEGER_DIGITS = Integer.MAX_VALUE;
4153 static final int MAXIMUM_FRACTION_DIGITS = Integer.MAX_VALUE;
4154
4155 // Proclaim JDK 1.1 serial compatibility.
4156 static final long serialVersionUID = 864413376551465018L;
4157
4158 /**
4159 * Cache to hold the NumberPattern of a Locale.
4160 */
4161 private static final ConcurrentMap<Locale, String> cachedLocaleData
4162 = new ConcurrentHashMap<>(3);
4163 }
|
37 */
38
39 package java.text;
40
41 import java.io.IOException;
42 import java.io.InvalidObjectException;
43 import java.io.ObjectInputStream;
44 import java.math.BigDecimal;
45 import java.math.BigInteger;
46 import java.math.RoundingMode;
47 import java.text.spi.NumberFormatProvider;
48 import java.util.ArrayList;
49 import java.util.Currency;
50 import java.util.Locale;
51 import java.util.ResourceBundle;
52 import java.util.concurrent.ConcurrentHashMap;
53 import java.util.concurrent.ConcurrentMap;
54 import java.util.concurrent.atomic.AtomicInteger;
55 import java.util.concurrent.atomic.AtomicLong;
56 import sun.util.locale.provider.LocaleProviderAdapter;
57 import sun.util.locale.provider.ResourceBundleBasedAdapter;
58
59 /**
60 * <code>DecimalFormat</code> is a concrete subclass of
61 * <code>NumberFormat</code> that formats decimal numbers. It has a variety of
62 * features designed to make it possible to parse and format numbers in any
63 * locale, including support for Western, Arabic, and Indic digits. It also
64 * supports different kinds of numbers, including integers (123), fixed-point
65 * numbers (123.4), scientific notation (1.23E4), percentages (12%), and
66 * currency amounts ($123). All of these can be localized.
67 *
68 * <p>To obtain a <code>NumberFormat</code> for a specific locale, including the
69 * default locale, call one of <code>NumberFormat</code>'s factory methods, such
70 * as <code>getInstance()</code>. In general, do not call the
71 * <code>DecimalFormat</code> constructors directly, since the
72 * <code>NumberFormat</code> factory methods may return subclasses other than
73 * <code>DecimalFormat</code>. If you need to customize the format object, do
74 * something like this:
75 *
76 * <blockquote><pre>
77 * NumberFormat f = NumberFormat.getInstance(loc);
378 * @author Alan Liu
379 */
380 public class DecimalFormat extends NumberFormat {
381
382 /**
383 * Creates a DecimalFormat using the default pattern and symbols
384 * for the default locale. This is a convenient way to obtain a
385 * DecimalFormat when internationalization is not the main concern.
386 * <p>
387 * To obtain standard formats for a given locale, use the factory methods
388 * on NumberFormat such as getNumberInstance. These factories will
389 * return the most appropriate sub-class of NumberFormat for a given
390 * locale.
391 *
392 * @see java.text.NumberFormat#getInstance
393 * @see java.text.NumberFormat#getNumberInstance
394 * @see java.text.NumberFormat#getCurrencyInstance
395 * @see java.text.NumberFormat#getPercentInstance
396 */
397 public DecimalFormat() {
398 // Get the pattern for the default locale.
399 Locale def = Locale.getDefault(Locale.Category.FORMAT);
400 LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class, def);
401 if (!(adapter instanceof ResourceBundleBasedAdapter)) {
402 adapter = LocaleProviderAdapter.getResourceBundleBased();
403 }
404 String[] all = adapter.getLocaleResources(def).getNumberPatterns();
405
406 // Always applyPattern after the symbols are set
407 this.symbols = DecimalFormatSymbols.getInstance(def);
408 applyPattern(all[0], false);
409 }
410
411
412 /**
413 * Creates a DecimalFormat using the given pattern and the symbols
414 * for the default locale. This is a convenient way to obtain a
415 * DecimalFormat when internationalization is not the main concern.
416 * <p>
417 * To obtain standard formats for a given locale, use the factory methods
418 * on NumberFormat such as getNumberInstance. These factories will
419 * return the most appropriate sub-class of NumberFormat for a given
420 * locale.
421 *
422 * @param pattern A non-localized pattern string.
423 * @exception NullPointerException if <code>pattern</code> is null
424 * @exception IllegalArgumentException if the given pattern is invalid.
425 * @see java.text.NumberFormat#getInstance
426 * @see java.text.NumberFormat#getNumberInstance
427 * @see java.text.NumberFormat#getCurrencyInstance
428 * @see java.text.NumberFormat#getPercentInstance
4127 * replaced with the monetary decimal separator.
4128 *
4129 * The CURRENCY_SIGN is not localized.
4130 */
4131 private static final char CURRENCY_SIGN = '\u00A4';
4132
4133 private static final char QUOTE = '\'';
4134
4135 private static FieldPosition[] EmptyFieldPositionArray = new FieldPosition[0];
4136
4137 // Upper limit on integer and fraction digits for a Java double
4138 static final int DOUBLE_INTEGER_DIGITS = 309;
4139 static final int DOUBLE_FRACTION_DIGITS = 340;
4140
4141 // Upper limit on integer and fraction digits for BigDecimal and BigInteger
4142 static final int MAXIMUM_INTEGER_DIGITS = Integer.MAX_VALUE;
4143 static final int MAXIMUM_FRACTION_DIGITS = Integer.MAX_VALUE;
4144
4145 // Proclaim JDK 1.1 serial compatibility.
4146 static final long serialVersionUID = 864413376551465018L;
4147 }
|