Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/NumberFormat.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.text;3940import java.io.InvalidObjectException;41import java.io.IOException;42import java.io.ObjectInputStream;43import java.io.ObjectOutputStream;44import java.math.BigInteger;45import java.math.RoundingMode;46import java.text.spi.NumberFormatProvider;47import java.util.Currency;48import java.util.HashMap;49import java.util.Hashtable;50import java.util.Locale;51import java.util.Map;52import java.util.ResourceBundle;53import java.util.concurrent.atomic.AtomicInteger;54import java.util.concurrent.atomic.AtomicLong;55import java.util.spi.LocaleServiceProvider;56import sun.util.locale.provider.LocaleProviderAdapter;57import sun.util.locale.provider.LocaleServiceProviderPool;5859/**60* <code>NumberFormat</code> is the abstract base class for all number61* formats. This class provides the interface for formatting and parsing62* numbers. <code>NumberFormat</code> also provides methods for determining63* which locales have number formats, and what their names are.64*65* <p>66* <code>NumberFormat</code> helps you to format and parse numbers for any locale.67* Your code can be completely independent of the locale conventions for68* decimal points, thousands-separators, or even the particular decimal69* digits used, or whether the number format is even decimal.70*71* <p>72* To format a number for the current Locale, use one of the factory73* class methods:74* <blockquote>75* <pre>{@code76* myString = NumberFormat.getInstance().format(myNumber);77* }</pre>78* </blockquote>79* If you are formatting multiple numbers, it is80* more efficient to get the format and use it multiple times so that81* the system doesn't have to fetch the information about the local82* language and country conventions multiple times.83* <blockquote>84* <pre>{@code85* NumberFormat nf = NumberFormat.getInstance();86* for (int i = 0; i < myNumber.length; ++i) {87* output.println(nf.format(myNumber[i]) + "; ");88* }89* }</pre>90* </blockquote>91* To format a number for a different Locale, specify it in the92* call to <code>getInstance</code>.93* <blockquote>94* <pre>{@code95* NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);96* }</pre>97* </blockquote>98* You can also use a <code>NumberFormat</code> to parse numbers:99* <blockquote>100* <pre>{@code101* myNumber = nf.parse(myString);102* }</pre>103* </blockquote>104* Use <code>getInstance</code> or <code>getNumberInstance</code> to get the105* normal number format. Use <code>getIntegerInstance</code> to get an106* integer number format. Use <code>getCurrencyInstance</code> to get the107* currency number format. And use <code>getPercentInstance</code> to get a108* format for displaying percentages. With this format, a fraction like109* 0.53 is displayed as 53%.110*111* <p>112* You can also control the display of numbers with such methods as113* <code>setMinimumFractionDigits</code>.114* If you want even more control over the format or parsing,115* or want to give your users more control,116* you can try casting the <code>NumberFormat</code> you get from the factory methods117* to a <code>DecimalFormat</code>. This will work for the vast majority118* of locales; just remember to put it in a <code>try</code> block in case you119* encounter an unusual one.120*121* <p>122* NumberFormat and DecimalFormat are designed such that some controls123* work for formatting and others work for parsing. The following is124* the detailed description for each these control methods,125* <p>126* setParseIntegerOnly : only affects parsing, e.g.127* if true, "3456.78" → 3456 (and leaves the parse position just after index 6)128* if false, "3456.78" → 3456.78 (and leaves the parse position just after index 8)129* This is independent of formatting. If you want to not show a decimal point130* where there might be no digits after the decimal point, use131* setDecimalSeparatorAlwaysShown.132* <p>133* setDecimalSeparatorAlwaysShown : only affects formatting, and only where134* there might be no digits after the decimal point, such as with a pattern135* like "#,##0.##", e.g.,136* if true, 3456.00 → "3,456."137* if false, 3456.00 → "3456"138* This is independent of parsing. If you want parsing to stop at the decimal139* point, use setParseIntegerOnly.140*141* <p>142* You can also use forms of the <code>parse</code> and <code>format</code>143* methods with <code>ParsePosition</code> and <code>FieldPosition</code> to144* allow you to:145* <ul>146* <li> progressively parse through pieces of a string147* <li> align the decimal point and other areas148* </ul>149* For example, you can align numbers in two ways:150* <ol>151* <li> If you are using a monospaced font with spacing for alignment,152* you can pass the <code>FieldPosition</code> in your format call, with153* <code>field</code> = <code>INTEGER_FIELD</code>. On output,154* <code>getEndIndex</code> will be set to the offset between the155* last character of the integer and the decimal. Add156* (desiredSpaceCount - getEndIndex) spaces at the front of the string.157*158* <li> If you are using proportional fonts,159* instead of padding with spaces, measure the width160* of the string in pixels from the start to <code>getEndIndex</code>.161* Then move the pen by162* (desiredPixelWidth - widthToAlignmentPoint) before drawing the text.163* It also works where there is no decimal, but possibly additional164* characters at the end, e.g., with parentheses in negative165* numbers: "(12)" for -12.166* </ol>167*168* <h3><a name="synchronization">Synchronization</a></h3>169*170* <p>171* Number formats are generally not synchronized.172* It is recommended to create separate format instances for each thread.173* If multiple threads access a format concurrently, it must be synchronized174* externally.175*176* @see DecimalFormat177* @see ChoiceFormat178* @author Mark Davis179* @author Helena Shih180*/181public abstract class NumberFormat extends Format {182183/**184* Field constant used to construct a FieldPosition object. Signifies that185* the position of the integer part of a formatted number should be returned.186* @see java.text.FieldPosition187*/188public static final int INTEGER_FIELD = 0;189190/**191* Field constant used to construct a FieldPosition object. Signifies that192* the position of the fraction part of a formatted number should be returned.193* @see java.text.FieldPosition194*/195public static final int FRACTION_FIELD = 1;196197/**198* Sole constructor. (For invocation by subclass constructors, typically199* implicit.)200*/201protected NumberFormat() {202}203204/**205* Formats a number and appends the resulting text to the given string206* buffer.207* The number can be of any subclass of {@link java.lang.Number}.208* <p>209* This implementation extracts the number's value using210* {@link java.lang.Number#longValue()} for all integral type values that211* can be converted to <code>long</code> without loss of information,212* including <code>BigInteger</code> values with a213* {@link java.math.BigInteger#bitLength() bit length} of less than 64,214* and {@link java.lang.Number#doubleValue()} for all other types. It215* then calls216* {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}217* or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.218* This may result in loss of magnitude information and precision for219* <code>BigInteger</code> and <code>BigDecimal</code> values.220* @param number the number to format221* @param toAppendTo the <code>StringBuffer</code> to which the formatted222* text is to be appended223* @param pos On input: an alignment field, if desired.224* On output: the offsets of the alignment field.225* @return the value passed in as <code>toAppendTo</code>226* @exception IllegalArgumentException if <code>number</code> is227* null or not an instance of <code>Number</code>.228* @exception NullPointerException if <code>toAppendTo</code> or229* <code>pos</code> is null230* @exception ArithmeticException if rounding is needed with rounding231* mode being set to RoundingMode.UNNECESSARY232* @see java.text.FieldPosition233*/234@Override235public StringBuffer format(Object number,236StringBuffer toAppendTo,237FieldPosition pos) {238if (number instanceof Long || number instanceof Integer ||239number instanceof Short || number instanceof Byte ||240number instanceof AtomicInteger || number instanceof AtomicLong ||241(number instanceof BigInteger &&242((BigInteger)number).bitLength() < 64)) {243return format(((Number)number).longValue(), toAppendTo, pos);244} else if (number instanceof Number) {245return format(((Number)number).doubleValue(), toAppendTo, pos);246} else {247throw new IllegalArgumentException("Cannot format given Object as a Number");248}249}250251/**252* Parses text from a string to produce a <code>Number</code>.253* <p>254* The method attempts to parse text starting at the index given by255* <code>pos</code>.256* If parsing succeeds, then the index of <code>pos</code> is updated257* to the index after the last character used (parsing does not necessarily258* use all characters up to the end of the string), and the parsed259* number is returned. The updated <code>pos</code> can be used to260* indicate the starting point for the next call to this method.261* If an error occurs, then the index of <code>pos</code> is not262* changed, the error index of <code>pos</code> is set to the index of263* the character where the error occurred, and null is returned.264* <p>265* See the {@link #parse(String, ParsePosition)} method for more information266* on number parsing.267*268* @param source A <code>String</code>, part of which should be parsed.269* @param pos A <code>ParsePosition</code> object with index and error270* index information as described above.271* @return A <code>Number</code> parsed from the string. In case of272* error, returns null.273* @exception NullPointerException if <code>pos</code> is null.274*/275@Override276public final Object parseObject(String source, ParsePosition pos) {277return parse(source, pos);278}279280/**281* Specialization of format.282*283* @param number the double number to format284* @return the formatted String285* @exception ArithmeticException if rounding is needed with rounding286* mode being set to RoundingMode.UNNECESSARY287* @see java.text.Format#format288*/289public final String format(double number) {290// Use fast-path for double result if that works291String result = fastFormat(number);292if (result != null)293return result;294295return format(number, new StringBuffer(),296DontCareFieldPosition.INSTANCE).toString();297}298299/*300* fastFormat() is supposed to be implemented in concrete subclasses only.301* Default implem always returns null.302*/303String fastFormat(double number) { return null; }304305/**306* Specialization of format.307*308* @param number the long number to format309* @return the formatted String310* @exception ArithmeticException if rounding is needed with rounding311* mode being set to RoundingMode.UNNECESSARY312* @see java.text.Format#format313*/314public final String format(long number) {315return format(number, new StringBuffer(),316DontCareFieldPosition.INSTANCE).toString();317}318319/**320* Specialization of format.321*322* @param number the double number to format323* @param toAppendTo the StringBuffer to which the formatted text is to be324* appended325* @param pos the field position326* @return the formatted StringBuffer327* @exception ArithmeticException if rounding is needed with rounding328* mode being set to RoundingMode.UNNECESSARY329* @see java.text.Format#format330*/331public abstract StringBuffer format(double number,332StringBuffer toAppendTo,333FieldPosition pos);334335/**336* Specialization of format.337*338* @param number the long number to format339* @param toAppendTo the StringBuffer to which the formatted text is to be340* appended341* @param pos the field position342* @return the formatted StringBuffer343* @exception ArithmeticException if rounding is needed with rounding344* mode being set to RoundingMode.UNNECESSARY345* @see java.text.Format#format346*/347public abstract StringBuffer format(long number,348StringBuffer toAppendTo,349FieldPosition pos);350351/**352* Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,353* Long.MAX_VALUE] and with no decimals), otherwise a Double.354* If IntegerOnly is set, will stop at a decimal355* point (or equivalent; e.g., for rational numbers "1 2/3", will stop356* after the 1).357* Does not throw an exception; if no object can be parsed, index is358* unchanged!359*360* @param source the String to parse361* @param parsePosition the parse position362* @return the parsed value363* @see java.text.NumberFormat#isParseIntegerOnly364* @see java.text.Format#parseObject365*/366public abstract Number parse(String source, ParsePosition parsePosition);367368/**369* Parses text from the beginning of the given string to produce a number.370* The method may not use the entire text of the given string.371* <p>372* See the {@link #parse(String, ParsePosition)} method for more information373* on number parsing.374*375* @param source A <code>String</code> whose beginning should be parsed.376* @return A <code>Number</code> parsed from the string.377* @exception ParseException if the beginning of the specified string378* cannot be parsed.379*/380public Number parse(String source) throws ParseException {381ParsePosition parsePosition = new ParsePosition(0);382Number result = parse(source, parsePosition);383if (parsePosition.index == 0) {384throw new ParseException("Unparseable number: \"" + source + "\"",385parsePosition.errorIndex);386}387return result;388}389390/**391* Returns true if this format will parse numbers as integers only.392* For example in the English locale, with ParseIntegerOnly true, the393* string "1234." would be parsed as the integer value 1234 and parsing394* would stop at the "." character. Of course, the exact format accepted395* by the parse operation is locale dependant and determined by sub-classes396* of NumberFormat.397*398* @return {@code true} if numbers should be parsed as integers only;399* {@code false} otherwise400*/401public boolean isParseIntegerOnly() {402return parseIntegerOnly;403}404405/**406* Sets whether or not numbers should be parsed as integers only.407*408* @param value {@code true} if numbers should be parsed as integers only;409* {@code false} otherwise410* @see #isParseIntegerOnly411*/412public void setParseIntegerOnly(boolean value) {413parseIntegerOnly = value;414}415416//============== Locale Stuff =====================417418/**419* Returns a general-purpose number format for the current default420* {@link java.util.Locale.Category#FORMAT FORMAT} locale.421* This is the same as calling422* {@link #getNumberInstance() getNumberInstance()}.423*424* @return the {@code NumberFormat} instance for general-purpose number425* formatting426*/427public final static NumberFormat getInstance() {428return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);429}430431/**432* Returns a general-purpose number format for the specified locale.433* This is the same as calling434* {@link #getNumberInstance(java.util.Locale) getNumberInstance(inLocale)}.435*436* @param inLocale the desired locale437* @return the {@code NumberFormat} instance for general-purpose number438* formatting439*/440public static NumberFormat getInstance(Locale inLocale) {441return getInstance(inLocale, NUMBERSTYLE);442}443444/**445* Returns a general-purpose number format for the current default446* {@link java.util.Locale.Category#FORMAT FORMAT} locale.447* <p>This is equivalent to calling448* {@link #getNumberInstance(Locale)449* getNumberInstance(Locale.getDefault(Locale.Category.FORMAT))}.450*451* @return the {@code NumberFormat} instance for general-purpose number452* formatting453* @see java.util.Locale#getDefault(java.util.Locale.Category)454* @see java.util.Locale.Category#FORMAT455*/456public final static NumberFormat getNumberInstance() {457return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);458}459460/**461* Returns a general-purpose number format for the specified locale.462*463* @param inLocale the desired locale464* @return the {@code NumberFormat} instance for general-purpose number465* formatting466*/467public static NumberFormat getNumberInstance(Locale inLocale) {468return getInstance(inLocale, NUMBERSTYLE);469}470471/**472* Returns an integer number format for the current default473* {@link java.util.Locale.Category#FORMAT FORMAT} locale. The474* returned number format is configured to round floating point numbers475* to the nearest integer using half-even rounding (see {@link476* java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,477* and to parse only the integer part of an input string (see {@link478* #isParseIntegerOnly isParseIntegerOnly}).479* <p>This is equivalent to calling480* {@link #getIntegerInstance(Locale)481* getIntegerInstance(Locale.getDefault(Locale.Category.FORMAT))}.482*483* @see #getRoundingMode()484* @see java.util.Locale#getDefault(java.util.Locale.Category)485* @see java.util.Locale.Category#FORMAT486* @return a number format for integer values487* @since 1.4488*/489public final static NumberFormat getIntegerInstance() {490return getInstance(Locale.getDefault(Locale.Category.FORMAT), INTEGERSTYLE);491}492493/**494* Returns an integer number format for the specified locale. The495* returned number format is configured to round floating point numbers496* to the nearest integer using half-even rounding (see {@link497* java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}) for formatting,498* and to parse only the integer part of an input string (see {@link499* #isParseIntegerOnly isParseIntegerOnly}).500*501* @param inLocale the desired locale502* @see #getRoundingMode()503* @return a number format for integer values504* @since 1.4505*/506public static NumberFormat getIntegerInstance(Locale inLocale) {507return getInstance(inLocale, INTEGERSTYLE);508}509510/**511* Returns a currency format for the current default512* {@link java.util.Locale.Category#FORMAT FORMAT} locale.513* <p>This is equivalent to calling514* {@link #getCurrencyInstance(Locale)515* getCurrencyInstance(Locale.getDefault(Locale.Category.FORMAT))}.516*517* @return the {@code NumberFormat} instance for currency formatting518* @see java.util.Locale#getDefault(java.util.Locale.Category)519* @see java.util.Locale.Category#FORMAT520*/521public final static NumberFormat getCurrencyInstance() {522return getInstance(Locale.getDefault(Locale.Category.FORMAT), CURRENCYSTYLE);523}524525/**526* Returns a currency format for the specified locale.527*528* @param inLocale the desired locale529* @return the {@code NumberFormat} instance for currency formatting530*/531public static NumberFormat getCurrencyInstance(Locale inLocale) {532return getInstance(inLocale, CURRENCYSTYLE);533}534535/**536* Returns a percentage format for the current default537* {@link java.util.Locale.Category#FORMAT FORMAT} locale.538* <p>This is equivalent to calling539* {@link #getPercentInstance(Locale)540* getPercentInstance(Locale.getDefault(Locale.Category.FORMAT))}.541*542* @return the {@code NumberFormat} instance for percentage formatting543* @see java.util.Locale#getDefault(java.util.Locale.Category)544* @see java.util.Locale.Category#FORMAT545*/546public final static NumberFormat getPercentInstance() {547return getInstance(Locale.getDefault(Locale.Category.FORMAT), PERCENTSTYLE);548}549550/**551* Returns a percentage format for the specified locale.552*553* @param inLocale the desired locale554* @return the {@code NumberFormat} instance for percentage formatting555*/556public static NumberFormat getPercentInstance(Locale inLocale) {557return getInstance(inLocale, PERCENTSTYLE);558}559560/**561* Returns a scientific format for the current default locale.562*/563/*public*/ final static NumberFormat getScientificInstance() {564return getInstance(Locale.getDefault(Locale.Category.FORMAT), SCIENTIFICSTYLE);565}566567/**568* Returns a scientific format for the specified locale.569*570* @param inLocale the desired locale571*/572/*public*/ static NumberFormat getScientificInstance(Locale inLocale) {573return getInstance(inLocale, SCIENTIFICSTYLE);574}575576/**577* Returns an array of all locales for which the578* <code>get*Instance</code> methods of this class can return579* localized instances.580* The returned array represents the union of locales supported by the Java581* runtime and by installed582* {@link java.text.spi.NumberFormatProvider NumberFormatProvider} implementations.583* It must contain at least a <code>Locale</code> instance equal to584* {@link java.util.Locale#US Locale.US}.585*586* @return An array of locales for which localized587* <code>NumberFormat</code> instances are available.588*/589public static Locale[] getAvailableLocales() {590LocaleServiceProviderPool pool =591LocaleServiceProviderPool.getPool(NumberFormatProvider.class);592return pool.getAvailableLocales();593}594595/**596* Overrides hashCode.597*/598@Override599public int hashCode() {600return maximumIntegerDigits * 37 + maxFractionDigits;601// just enough fields for a reasonable distribution602}603604/**605* Overrides equals.606*/607@Override608public boolean equals(Object obj) {609if (obj == null) {610return false;611}612if (this == obj) {613return true;614}615if (getClass() != obj.getClass()) {616return false;617}618NumberFormat other = (NumberFormat) obj;619return (maximumIntegerDigits == other.maximumIntegerDigits620&& minimumIntegerDigits == other.minimumIntegerDigits621&& maximumFractionDigits == other.maximumFractionDigits622&& minimumFractionDigits == other.minimumFractionDigits623&& groupingUsed == other.groupingUsed624&& parseIntegerOnly == other.parseIntegerOnly);625}626627/**628* Overrides Cloneable.629*/630@Override631public Object clone() {632NumberFormat other = (NumberFormat) super.clone();633return other;634}635636/**637* Returns true if grouping is used in this format. For example, in the638* English locale, with grouping on, the number 1234567 might be formatted639* as "1,234,567". The grouping separator as well as the size of each group640* is locale dependant and is determined by sub-classes of NumberFormat.641*642* @return {@code true} if grouping is used;643* {@code false} otherwise644* @see #setGroupingUsed645*/646public boolean isGroupingUsed() {647return groupingUsed;648}649650/**651* Set whether or not grouping will be used in this format.652*653* @param newValue {@code true} if grouping is used;654* {@code false} otherwise655* @see #isGroupingUsed656*/657public void setGroupingUsed(boolean newValue) {658groupingUsed = newValue;659}660661/**662* Returns the maximum number of digits allowed in the integer portion of a663* number.664*665* @return the maximum number of digits666* @see #setMaximumIntegerDigits667*/668public int getMaximumIntegerDigits() {669return maximumIntegerDigits;670}671672/**673* Sets the maximum number of digits allowed in the integer portion of a674* number. maximumIntegerDigits must be ≥ minimumIntegerDigits. If the675* new value for maximumIntegerDigits is less than the current value676* of minimumIntegerDigits, then minimumIntegerDigits will also be set to677* the new value.678*679* @param newValue the maximum number of integer digits to be shown; if680* less than zero, then zero is used. The concrete subclass may enforce an681* upper limit to this value appropriate to the numeric type being formatted.682* @see #getMaximumIntegerDigits683*/684public void setMaximumIntegerDigits(int newValue) {685maximumIntegerDigits = Math.max(0,newValue);686if (minimumIntegerDigits > maximumIntegerDigits) {687minimumIntegerDigits = maximumIntegerDigits;688}689}690691/**692* Returns the minimum number of digits allowed in the integer portion of a693* number.694*695* @return the minimum number of digits696* @see #setMinimumIntegerDigits697*/698public int getMinimumIntegerDigits() {699return minimumIntegerDigits;700}701702/**703* Sets the minimum number of digits allowed in the integer portion of a704* number. minimumIntegerDigits must be ≤ maximumIntegerDigits. If the705* new value for minimumIntegerDigits exceeds the current value706* of maximumIntegerDigits, then maximumIntegerDigits will also be set to707* the new value708*709* @param newValue the minimum number of integer digits to be shown; if710* less than zero, then zero is used. The concrete subclass may enforce an711* upper limit to this value appropriate to the numeric type being formatted.712* @see #getMinimumIntegerDigits713*/714public void setMinimumIntegerDigits(int newValue) {715minimumIntegerDigits = Math.max(0,newValue);716if (minimumIntegerDigits > maximumIntegerDigits) {717maximumIntegerDigits = minimumIntegerDigits;718}719}720721/**722* Returns the maximum number of digits allowed in the fraction portion of a723* number.724*725* @return the maximum number of digits.726* @see #setMaximumFractionDigits727*/728public int getMaximumFractionDigits() {729return maximumFractionDigits;730}731732/**733* Sets the maximum number of digits allowed in the fraction portion of a734* number. maximumFractionDigits must be ≥ minimumFractionDigits. If the735* new value for maximumFractionDigits is less than the current value736* of minimumFractionDigits, then minimumFractionDigits will also be set to737* the new value.738*739* @param newValue the maximum number of fraction digits to be shown; if740* less than zero, then zero is used. The concrete subclass may enforce an741* upper limit to this value appropriate to the numeric type being formatted.742* @see #getMaximumFractionDigits743*/744public void setMaximumFractionDigits(int newValue) {745maximumFractionDigits = Math.max(0,newValue);746if (maximumFractionDigits < minimumFractionDigits) {747minimumFractionDigits = maximumFractionDigits;748}749}750751/**752* Returns the minimum number of digits allowed in the fraction portion of a753* number.754*755* @return the minimum number of digits756* @see #setMinimumFractionDigits757*/758public int getMinimumFractionDigits() {759return minimumFractionDigits;760}761762/**763* Sets the minimum number of digits allowed in the fraction portion of a764* number. minimumFractionDigits must be ≤ maximumFractionDigits. If the765* new value for minimumFractionDigits exceeds the current value766* of maximumFractionDigits, then maximumIntegerDigits will also be set to767* the new value768*769* @param newValue the minimum number of fraction digits to be shown; if770* less than zero, then zero is used. The concrete subclass may enforce an771* upper limit to this value appropriate to the numeric type being formatted.772* @see #getMinimumFractionDigits773*/774public void setMinimumFractionDigits(int newValue) {775minimumFractionDigits = Math.max(0,newValue);776if (maximumFractionDigits < minimumFractionDigits) {777maximumFractionDigits = minimumFractionDigits;778}779}780781/**782* Gets the currency used by this number format when formatting783* currency values. The initial value is derived in a locale dependent784* way. The returned value may be null if no valid785* currency could be determined and no currency has been set using786* {@link #setCurrency(java.util.Currency) setCurrency}.787* <p>788* The default implementation throws789* <code>UnsupportedOperationException</code>.790*791* @return the currency used by this number format, or <code>null</code>792* @exception UnsupportedOperationException if the number format class793* doesn't implement currency formatting794* @since 1.4795*/796public Currency getCurrency() {797throw new UnsupportedOperationException();798}799800/**801* Sets the currency used by this number format when formatting802* currency values. This does not update the minimum or maximum803* number of fraction digits used by the number format.804* <p>805* The default implementation throws806* <code>UnsupportedOperationException</code>.807*808* @param currency the new currency to be used by this number format809* @exception UnsupportedOperationException if the number format class810* doesn't implement currency formatting811* @exception NullPointerException if <code>currency</code> is null812* @since 1.4813*/814public void setCurrency(Currency currency) {815throw new UnsupportedOperationException();816}817818/**819* Gets the {@link java.math.RoundingMode} used in this NumberFormat.820* The default implementation of this method in NumberFormat821* always throws {@link java.lang.UnsupportedOperationException}.822* Subclasses which handle different rounding modes should override823* this method.824*825* @exception UnsupportedOperationException The default implementation826* always throws this exception827* @return The <code>RoundingMode</code> used for this NumberFormat.828* @see #setRoundingMode(RoundingMode)829* @since 1.6830*/831public RoundingMode getRoundingMode() {832throw new UnsupportedOperationException();833}834835/**836* Sets the {@link java.math.RoundingMode} used in this NumberFormat.837* The default implementation of this method in NumberFormat always838* throws {@link java.lang.UnsupportedOperationException}.839* Subclasses which handle different rounding modes should override840* this method.841*842* @exception UnsupportedOperationException The default implementation843* always throws this exception844* @exception NullPointerException if <code>roundingMode</code> is null845* @param roundingMode The <code>RoundingMode</code> to be used846* @see #getRoundingMode()847* @since 1.6848*/849public void setRoundingMode(RoundingMode roundingMode) {850throw new UnsupportedOperationException();851}852853// =======================privates===============================854855private static NumberFormat getInstance(Locale desiredLocale,856int choice) {857LocaleProviderAdapter adapter;858adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class,859desiredLocale);860NumberFormat numberFormat = getInstance(adapter, desiredLocale, choice);861if (numberFormat == null) {862numberFormat = getInstance(LocaleProviderAdapter.forJRE(),863desiredLocale, choice);864}865return numberFormat;866}867868private static NumberFormat getInstance(LocaleProviderAdapter adapter,869Locale locale, int choice) {870NumberFormatProvider provider = adapter.getNumberFormatProvider();871NumberFormat numberFormat = null;872switch (choice) {873case NUMBERSTYLE:874numberFormat = provider.getNumberInstance(locale);875break;876case PERCENTSTYLE:877numberFormat = provider.getPercentInstance(locale);878break;879case CURRENCYSTYLE:880numberFormat = provider.getCurrencyInstance(locale);881break;882case INTEGERSTYLE:883numberFormat = provider.getIntegerInstance(locale);884break;885}886return numberFormat;887}888889/**890* First, read in the default serializable data.891*892* Then, if <code>serialVersionOnStream</code> is less than 1, indicating that893* the stream was written by JDK 1.1,894* set the <code>int</code> fields such as <code>maximumIntegerDigits</code>895* to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,896* since the <code>int</code> fields were not present in JDK 1.1.897* Finally, set serialVersionOnStream back to the maximum allowed value so that898* default serialization will work properly if this object is streamed out again.899*900* <p>If <code>minimumIntegerDigits</code> is greater than901* <code>maximumIntegerDigits</code> or <code>minimumFractionDigits</code>902* is greater than <code>maximumFractionDigits</code>, then the stream data903* is invalid and this method throws an <code>InvalidObjectException</code>.904* In addition, if any of these values is negative, then this method throws905* an <code>InvalidObjectException</code>.906*907* @since 1.2908*/909private void readObject(ObjectInputStream stream)910throws IOException, ClassNotFoundException911{912stream.defaultReadObject();913if (serialVersionOnStream < 1) {914// Didn't have additional int fields, reassign to use them.915maximumIntegerDigits = maxIntegerDigits;916minimumIntegerDigits = minIntegerDigits;917maximumFractionDigits = maxFractionDigits;918minimumFractionDigits = minFractionDigits;919}920if (minimumIntegerDigits > maximumIntegerDigits ||921minimumFractionDigits > maximumFractionDigits ||922minimumIntegerDigits < 0 || minimumFractionDigits < 0) {923throw new InvalidObjectException("Digit count range invalid");924}925serialVersionOnStream = currentSerialVersion;926}927928/**929* Write out the default serializable data, after first setting930* the <code>byte</code> fields such as <code>maxIntegerDigits</code> to be931* equal to the <code>int</code> fields such as <code>maximumIntegerDigits</code>932* (or to <code>Byte.MAX_VALUE</code>, whichever is smaller), for compatibility933* with the JDK 1.1 version of the stream format.934*935* @since 1.2936*/937private void writeObject(ObjectOutputStream stream)938throws IOException939{940maxIntegerDigits = (maximumIntegerDigits > Byte.MAX_VALUE) ?941Byte.MAX_VALUE : (byte)maximumIntegerDigits;942minIntegerDigits = (minimumIntegerDigits > Byte.MAX_VALUE) ?943Byte.MAX_VALUE : (byte)minimumIntegerDigits;944maxFractionDigits = (maximumFractionDigits > Byte.MAX_VALUE) ?945Byte.MAX_VALUE : (byte)maximumFractionDigits;946minFractionDigits = (minimumFractionDigits > Byte.MAX_VALUE) ?947Byte.MAX_VALUE : (byte)minimumFractionDigits;948stream.defaultWriteObject();949}950951// Constants used by factory methods to specify a style of format.952private static final int NUMBERSTYLE = 0;953private static final int CURRENCYSTYLE = 1;954private static final int PERCENTSTYLE = 2;955private static final int SCIENTIFICSTYLE = 3;956private static final int INTEGERSTYLE = 4;957958/**959* True if the grouping (i.e. thousands) separator is used when960* formatting and parsing numbers.961*962* @serial963* @see #isGroupingUsed964*/965private boolean groupingUsed = true;966967/**968* The maximum number of digits allowed in the integer portion of a969* number. <code>maxIntegerDigits</code> must be greater than or equal to970* <code>minIntegerDigits</code>.971* <p>972* <strong>Note:</strong> This field exists only for serialization973* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new974* <code>int</code> field <code>maximumIntegerDigits</code> is used instead.975* When writing to a stream, <code>maxIntegerDigits</code> is set to976* <code>maximumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,977* whichever is smaller. When reading from a stream, this field is used978* only if <code>serialVersionOnStream</code> is less than 1.979*980* @serial981* @see #getMaximumIntegerDigits982*/983private byte maxIntegerDigits = 40;984985/**986* The minimum number of digits allowed in the integer portion of a987* number. <code>minimumIntegerDigits</code> must be less than or equal to988* <code>maximumIntegerDigits</code>.989* <p>990* <strong>Note:</strong> This field exists only for serialization991* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new992* <code>int</code> field <code>minimumIntegerDigits</code> is used instead.993* When writing to a stream, <code>minIntegerDigits</code> is set to994* <code>minimumIntegerDigits</code> or <code>Byte.MAX_VALUE</code>,995* whichever is smaller. When reading from a stream, this field is used996* only if <code>serialVersionOnStream</code> is less than 1.997*998* @serial999* @see #getMinimumIntegerDigits1000*/1001private byte minIntegerDigits = 1;10021003/**1004* The maximum number of digits allowed in the fractional portion of a1005* number. <code>maximumFractionDigits</code> must be greater than or equal to1006* <code>minimumFractionDigits</code>.1007* <p>1008* <strong>Note:</strong> This field exists only for serialization1009* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new1010* <code>int</code> field <code>maximumFractionDigits</code> is used instead.1011* When writing to a stream, <code>maxFractionDigits</code> is set to1012* <code>maximumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,1013* whichever is smaller. When reading from a stream, this field is used1014* only if <code>serialVersionOnStream</code> is less than 1.1015*1016* @serial1017* @see #getMaximumFractionDigits1018*/1019private byte maxFractionDigits = 3; // invariant, >= minFractionDigits10201021/**1022* The minimum number of digits allowed in the fractional portion of a1023* number. <code>minimumFractionDigits</code> must be less than or equal to1024* <code>maximumFractionDigits</code>.1025* <p>1026* <strong>Note:</strong> This field exists only for serialization1027* compatibility with JDK 1.1. In Java platform 2 v1.2 and higher, the new1028* <code>int</code> field <code>minimumFractionDigits</code> is used instead.1029* When writing to a stream, <code>minFractionDigits</code> is set to1030* <code>minimumFractionDigits</code> or <code>Byte.MAX_VALUE</code>,1031* whichever is smaller. When reading from a stream, this field is used1032* only if <code>serialVersionOnStream</code> is less than 1.1033*1034* @serial1035* @see #getMinimumFractionDigits1036*/1037private byte minFractionDigits = 0;10381039/**1040* True if this format will parse numbers as integers only.1041*1042* @serial1043* @see #isParseIntegerOnly1044*/1045private boolean parseIntegerOnly = false;10461047// new fields for 1.2. byte is too small for integer digits.10481049/**1050* The maximum number of digits allowed in the integer portion of a1051* number. <code>maximumIntegerDigits</code> must be greater than or equal to1052* <code>minimumIntegerDigits</code>.1053*1054* @serial1055* @since 1.21056* @see #getMaximumIntegerDigits1057*/1058private int maximumIntegerDigits = 40;10591060/**1061* The minimum number of digits allowed in the integer portion of a1062* number. <code>minimumIntegerDigits</code> must be less than or equal to1063* <code>maximumIntegerDigits</code>.1064*1065* @serial1066* @since 1.21067* @see #getMinimumIntegerDigits1068*/1069private int minimumIntegerDigits = 1;10701071/**1072* The maximum number of digits allowed in the fractional portion of a1073* number. <code>maximumFractionDigits</code> must be greater than or equal to1074* <code>minimumFractionDigits</code>.1075*1076* @serial1077* @since 1.21078* @see #getMaximumFractionDigits1079*/1080private int maximumFractionDigits = 3; // invariant, >= minFractionDigits10811082/**1083* The minimum number of digits allowed in the fractional portion of a1084* number. <code>minimumFractionDigits</code> must be less than or equal to1085* <code>maximumFractionDigits</code>.1086*1087* @serial1088* @since 1.21089* @see #getMinimumFractionDigits1090*/1091private int minimumFractionDigits = 0;10921093static final int currentSerialVersion = 1;10941095/**1096* Describes the version of <code>NumberFormat</code> present on the stream.1097* Possible values are:1098* <ul>1099* <li><b>0</b> (or uninitialized): the JDK 1.1 version of the stream format.1100* In this version, the <code>int</code> fields such as1101* <code>maximumIntegerDigits</code> were not present, and the <code>byte</code>1102* fields such as <code>maxIntegerDigits</code> are used instead.1103*1104* <li><b>1</b>: the 1.2 version of the stream format. The values of the1105* <code>byte</code> fields such as <code>maxIntegerDigits</code> are ignored,1106* and the <code>int</code> fields such as <code>maximumIntegerDigits</code>1107* are used instead.1108* </ul>1109* When streaming out a <code>NumberFormat</code>, the most recent format1110* (corresponding to the highest allowable <code>serialVersionOnStream</code>)1111* is always written.1112*1113* @serial1114* @since 1.21115*/1116private int serialVersionOnStream = currentSerialVersion;11171118// Removed "implements Cloneable" clause. Needs to update serialization1119// ID for backward compatibility.1120static final long serialVersionUID = -2308460125733713944L;112111221123//1124// class for AttributedCharacterIterator attributes1125//1126/**1127* Defines constants that are used as attribute keys in the1128* <code>AttributedCharacterIterator</code> returned1129* from <code>NumberFormat.formatToCharacterIterator</code> and as1130* field identifiers in <code>FieldPosition</code>.1131*1132* @since 1.41133*/1134public static class Field extends Format.Field {11351136// Proclaim serial compatibility with 1.4 FCS1137private static final long serialVersionUID = 7494728892700160890L;11381139// table of all instances in this class, used by readResolve1140private static final Map<String, Field> instanceMap = new HashMap<>(11);11411142/**1143* Creates a Field instance with the specified1144* name.1145*1146* @param name Name of the attribute1147*/1148protected Field(String name) {1149super(name);1150if (this.getClass() == NumberFormat.Field.class) {1151instanceMap.put(name, this);1152}1153}11541155/**1156* Resolves instances being deserialized to the predefined constants.1157*1158* @throws InvalidObjectException if the constant could not be resolved.1159* @return resolved NumberFormat.Field constant1160*/1161@Override1162protected Object readResolve() throws InvalidObjectException {1163if (this.getClass() != NumberFormat.Field.class) {1164throw new InvalidObjectException("subclass didn't correctly implement readResolve");1165}11661167Object instance = instanceMap.get(getName());1168if (instance != null) {1169return instance;1170} else {1171throw new InvalidObjectException("unknown attribute name");1172}1173}11741175/**1176* Constant identifying the integer field.1177*/1178public static final Field INTEGER = new Field("integer");11791180/**1181* Constant identifying the fraction field.1182*/1183public static final Field FRACTION = new Field("fraction");11841185/**1186* Constant identifying the exponent field.1187*/1188public static final Field EXPONENT = new Field("exponent");11891190/**1191* Constant identifying the decimal separator field.1192*/1193public static final Field DECIMAL_SEPARATOR =1194new Field("decimal separator");11951196/**1197* Constant identifying the sign field.1198*/1199public static final Field SIGN = new Field("sign");12001201/**1202* Constant identifying the grouping separator field.1203*/1204public static final Field GROUPING_SEPARATOR =1205new Field("grouping separator");12061207/**1208* Constant identifying the exponent symbol field.1209*/1210public static final Field EXPONENT_SYMBOL = new1211Field("exponent symbol");12121213/**1214* Constant identifying the percent field.1215*/1216public static final Field PERCENT = new Field("percent");12171218/**1219* Constant identifying the permille field.1220*/1221public static final Field PERMILLE = new Field("per mille");12221223/**1224* Constant identifying the currency field.1225*/1226public static final Field CURRENCY = new Field("currency");12271228/**1229* Constant identifying the exponent sign field.1230*/1231public static final Field EXPONENT_SIGN = new Field("exponent sign");1232}1233}123412351236