< prev index next >

src/java.base/share/classes/java/text/NumberFormat.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 45,61 **** import java.math.BigInteger; import java.math.RoundingMode; import java.text.spi.NumberFormatProvider; import java.util.Currency; import java.util.HashMap; - import java.util.Hashtable; import java.util.Locale; import java.util.Map; ! import java.util.ResourceBundle; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; - import java.util.spi.LocaleServiceProvider; import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleServiceProviderPool; /** * <code>NumberFormat</code> is the abstract base class for all number --- 45,59 ---- import java.math.BigInteger; import java.math.RoundingMode; import java.text.spi.NumberFormatProvider; import java.util.Currency; import java.util.HashMap; import java.util.Locale; import java.util.Map; ! import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import sun.util.locale.provider.LocaleProviderAdapter; import sun.util.locale.provider.LocaleServiceProviderPool; /** * <code>NumberFormat</code> is the abstract base class for all number
*** 110,132 **** * }</pre> * </blockquote> * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the * normal number format. Use <code>getIntegerInstance</code> to get an * integer number format. Use <code>getCurrencyInstance</code> to get the ! * currency number format. And use <code>getPercentInstance</code> to get a ! * format for displaying percentages. With this format, a fraction like ! * 0.53 is displayed as 53%. * * <p> * You can also control the display of numbers with such methods as * <code>setMinimumFractionDigits</code>. * If you want even more control over the format or parsing, * or want to give your users more control, * you can try casting the <code>NumberFormat</code> you get from the factory methods ! * to a <code>DecimalFormat</code>. This will work for the vast majority ! * of locales; just remember to put it in a <code>try</code> block in case you ! * encounter an unusual one. * * <p> * NumberFormat and DecimalFormat are designed such that some controls * work for formatting and others work for parsing. The following is * the detailed description for each these control methods, --- 108,134 ---- * }</pre> * </blockquote> * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the * normal number format. Use <code>getIntegerInstance</code> to get an * integer number format. Use <code>getCurrencyInstance</code> to get the ! * currency number format. Use {@code getCompactNumberInstance} to get the ! * compact number format to format a number in shorter form. For example, ! * {@code 2000} can be formatted as {@code "2K"} in ! * {@link java.util.Locale#US US locale}. Use <code>getPercentInstance</code> ! * to get a format for displaying percentages. With this format, a fraction ! * like 0.53 is displayed as 53%. * * <p> * You can also control the display of numbers with such methods as * <code>setMinimumFractionDigits</code>. * If you want even more control over the format or parsing, * or want to give your users more control, * you can try casting the <code>NumberFormat</code> you get from the factory methods ! * to a {@code DecimalFormat} or {@code CompactNumberFormat} depending on ! * the factory method used. This will work for the vast majority of locales; ! * just remember to put it in a <code>try</code> block in case you encounter ! * an unusual one. * * <p> * NumberFormat and DecimalFormat are designed such that some controls * work for formatting and others work for parsing. The following is * the detailed description for each these control methods,
*** 199,208 **** --- 201,211 ---- * rounding (see {@link java.math.RoundingMode#HALF_EVEN * RoundingMode.HALF_EVEN}) for formatting. * * @see DecimalFormat * @see ChoiceFormat + * @see CompactNumberFormat * @author Mark Davis * @author Helena Shih * @since 1.1 */ public abstract class NumberFormat extends Format {
*** 470,480 **** * * @return the {@code NumberFormat} instance for general-purpose number * formatting */ public static final NumberFormat getInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE); } /** * Returns a general-purpose number format for the specified locale. * This is the same as calling --- 473,483 ---- * * @return the {@code NumberFormat} instance for general-purpose number * formatting */ public static final NumberFormat getInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, NUMBERSTYLE); } /** * Returns a general-purpose number format for the specified locale. * This is the same as calling
*** 483,493 **** * @param inLocale the desired locale * @return the {@code NumberFormat} instance for general-purpose number * formatting */ public static NumberFormat getInstance(Locale inLocale) { ! return getInstance(inLocale, NUMBERSTYLE); } /** * Returns a general-purpose number format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale. --- 486,496 ---- * @param inLocale the desired locale * @return the {@code NumberFormat} instance for general-purpose number * formatting */ public static NumberFormat getInstance(Locale inLocale) { ! return getInstance(inLocale, null, NUMBERSTYLE); } /** * Returns a general-purpose number format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
*** 499,520 **** * formatting * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT */ public static final NumberFormat getNumberInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE); } /** * Returns a general-purpose number format for the specified locale. * * @param inLocale the desired locale * @return the {@code NumberFormat} instance for general-purpose number * formatting */ public static NumberFormat getNumberInstance(Locale inLocale) { ! return getInstance(inLocale, NUMBERSTYLE); } /** * Returns an integer number format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale. The --- 502,523 ---- * formatting * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT */ public static final NumberFormat getNumberInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, NUMBERSTYLE); } /** * Returns a general-purpose number format for the specified locale. * * @param inLocale the desired locale * @return the {@code NumberFormat} instance for general-purpose number * formatting */ public static NumberFormat getNumberInstance(Locale inLocale) { ! return getInstance(inLocale, null, NUMBERSTYLE); } /** * Returns an integer number format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale. The
*** 532,542 **** * @see java.util.Locale.Category#FORMAT * @return a number format for integer values * @since 1.4 */ public static final NumberFormat getIntegerInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), INTEGERSTYLE); } /** * Returns an integer number format for the specified locale. The * returned number format is configured to round floating point numbers --- 535,545 ---- * @see java.util.Locale.Category#FORMAT * @return a number format for integer values * @since 1.4 */ public static final NumberFormat getIntegerInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, INTEGERSTYLE); } /** * Returns an integer number format for the specified locale. The * returned number format is configured to round floating point numbers
*** 549,559 **** * @see #getRoundingMode() * @return a number format for integer values * @since 1.4 */ public static NumberFormat getIntegerInstance(Locale inLocale) { ! return getInstance(inLocale, INTEGERSTYLE); } /** * Returns a currency format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale. --- 552,562 ---- * @see #getRoundingMode() * @return a number format for integer values * @since 1.4 */ public static NumberFormat getIntegerInstance(Locale inLocale) { ! return getInstance(inLocale, null, INTEGERSTYLE); } /** * Returns a currency format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
*** 564,584 **** * @return the {@code NumberFormat} instance for currency formatting * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT */ public static final NumberFormat getCurrencyInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), CURRENCYSTYLE); } /** * Returns a currency format for the specified locale. * * @param inLocale the desired locale * @return the {@code NumberFormat} instance for currency formatting */ public static NumberFormat getCurrencyInstance(Locale inLocale) { ! return getInstance(inLocale, CURRENCYSTYLE); } /** * Returns a percentage format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale. --- 567,587 ---- * @return the {@code NumberFormat} instance for currency formatting * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT */ public static final NumberFormat getCurrencyInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, CURRENCYSTYLE); } /** * Returns a currency format for the specified locale. * * @param inLocale the desired locale * @return the {@code NumberFormat} instance for currency formatting */ public static NumberFormat getCurrencyInstance(Locale inLocale) { ! return getInstance(inLocale, null, CURRENCYSTYLE); } /** * Returns a percentage format for the current default * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
*** 589,625 **** * @return the {@code NumberFormat} instance for percentage formatting * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT */ public static final NumberFormat getPercentInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), PERCENTSTYLE); } /** * Returns a percentage format for the specified locale. * * @param inLocale the desired locale * @return the {@code NumberFormat} instance for percentage formatting */ public static NumberFormat getPercentInstance(Locale inLocale) { ! return getInstance(inLocale, PERCENTSTYLE); } /** * Returns a scientific format for the current default locale. */ /*public*/ final static NumberFormat getScientificInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), SCIENTIFICSTYLE); } /** * Returns a scientific format for the specified locale. * * @param inLocale the desired locale */ /*public*/ static NumberFormat getScientificInstance(Locale inLocale) { ! return getInstance(inLocale, SCIENTIFICSTYLE); } /** * Returns an array of all locales for which the * <code>get*Instance</code> methods of this class can return --- 592,671 ---- * @return the {@code NumberFormat} instance for percentage formatting * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT */ public static final NumberFormat getPercentInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, PERCENTSTYLE); } /** * Returns a percentage format for the specified locale. * * @param inLocale the desired locale * @return the {@code NumberFormat} instance for percentage formatting */ public static NumberFormat getPercentInstance(Locale inLocale) { ! return getInstance(inLocale, null, PERCENTSTYLE); } /** * Returns a scientific format for the current default locale. */ /*public*/ final static NumberFormat getScientificInstance() { ! return getInstance(Locale.getDefault(Locale.Category.FORMAT), null, SCIENTIFICSTYLE); } /** * Returns a scientific format for the specified locale. * * @param inLocale the desired locale */ /*public*/ static NumberFormat getScientificInstance(Locale inLocale) { ! return getInstance(inLocale, null, SCIENTIFICSTYLE); ! } ! ! /** ! * Returns a compact number format for the default ! * {@link java.util.Locale.Category#FORMAT FORMAT} locale with ! * {@link NumberFormat.Style#SHORT "SHORT"} format style. ! * ! * @return A {@code NumberFormat} instance for compact number ! * formatting ! * ! * @see CompactNumberFormat ! * @see NumberFormat.Style ! * @see java.util.Locale#getDefault(java.util.Locale.Category) ! * @see java.util.Locale.Category#FORMAT ! * @since 12 ! */ ! public static NumberFormat getCompactNumberInstance() { ! return getInstance(Locale.getDefault( ! Locale.Category.FORMAT), NumberFormat.Style.SHORT, COMPACTSTYLE); ! } ! ! /** ! * Returns a compact number format for the specified {@link java.util.Locale locale} ! * and {@link NumberFormat.Style formatStyle}. ! * ! * @param locale the desired locale ! * @param formatStyle the style for formatting a number ! * @return A {@code NumberFormat} instance for compact number ! * formatting ! * @throws NullPointerException if {@code locale} or {@code formatStyle} ! * is {@code null} ! * ! * @see CompactNumberFormat ! * @see NumberFormat.Style ! * @see java.util.Locale ! * @since 12 ! */ ! public static NumberFormat getCompactNumberInstance(Locale locale, ! NumberFormat.Style formatStyle) { ! ! Objects.requireNonNull(locale); ! Objects.requireNonNull(formatStyle); ! return getInstance(locale, formatStyle, COMPACTSTYLE); } /** * Returns an array of all locales for which the * <code>get*Instance</code> methods of this class can return
*** 898,921 **** } // =======================privates=============================== private static NumberFormat getInstance(Locale desiredLocale, ! int choice) { LocaleProviderAdapter adapter; adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class, desiredLocale); ! NumberFormat numberFormat = getInstance(adapter, desiredLocale, choice); if (numberFormat == null) { numberFormat = getInstance(LocaleProviderAdapter.forJRE(), ! desiredLocale, choice); } return numberFormat; } private static NumberFormat getInstance(LocaleProviderAdapter adapter, ! Locale locale, int choice) { NumberFormatProvider provider = adapter.getNumberFormatProvider(); NumberFormat numberFormat = null; switch (choice) { case NUMBERSTYLE: numberFormat = provider.getNumberInstance(locale); --- 944,969 ---- } // =======================privates=============================== private static NumberFormat getInstance(Locale desiredLocale, ! Style formatStyle, int choice) { LocaleProviderAdapter adapter; adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class, desiredLocale); ! NumberFormat numberFormat = getInstance(adapter, desiredLocale, ! formatStyle, choice); if (numberFormat == null) { numberFormat = getInstance(LocaleProviderAdapter.forJRE(), ! desiredLocale, formatStyle, choice); } return numberFormat; } private static NumberFormat getInstance(LocaleProviderAdapter adapter, ! Locale locale, Style formatStyle, ! int choice) { NumberFormatProvider provider = adapter.getNumberFormatProvider(); NumberFormat numberFormat = null; switch (choice) { case NUMBERSTYLE: numberFormat = provider.getNumberInstance(locale);
*** 927,936 **** --- 975,987 ---- numberFormat = provider.getCurrencyInstance(locale); break; case INTEGERSTYLE: numberFormat = provider.getIntegerInstance(locale); break; + case COMPACTSTYLE: + numberFormat = provider.getCompactNumberInstance(locale, formatStyle); + break; } return numberFormat; } /**
*** 999,1008 **** --- 1050,1060 ---- private static final int NUMBERSTYLE = 0; private static final int CURRENCYSTYLE = 1; private static final int PERCENTSTYLE = 2; private static final int SCIENTIFICSTYLE = 3; private static final int INTEGERSTYLE = 4; + private static final int COMPACTSTYLE = 5; /** * True if the grouping (i.e. thousands) separator is used when * formatting and parsing numbers. *
*** 1274,1280 **** --- 1326,1370 ---- /** * Constant identifying the exponent sign field. */ public static final Field EXPONENT_SIGN = new Field("exponent sign"); + + /** + * Constant identifying the prefix field. + * + * @since 12 + */ + public static final Field PREFIX = new Field("prefix"); + + /** + * Constant identifying the suffix field. + * + * @since 12 + */ + public static final Field SUFFIX = new Field("suffix"); + } + + /** + * A number format style. + * <p> + * {@code Style} is an enum which represents the style for formatting + * a number within a given {@code NumberFormat} instance. + * + * @see CompactNumberFormat + * @see NumberFormat#getCompactNumberInstance(Locale, Style) + * @since 12 + */ + public enum Style { + + /** + * The {@code SHORT} number format style. + */ + SHORT, + + /** + * The {@code LONG} number format style. + */ + LONG + } }
< prev index next >