/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
*
* The original version of this source code and documentation is copyrighted
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
* materials are provided under terms of a License Agreement between Taligent
* and Sun. This technology is protected by multiple US and International
* patents. This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
/**
* <code>DecimalFormat</code> is a concrete subclass of
* <code>NumberFormat</code> that formats decimal numbers. It has a variety of
* features designed to make it possible to parse and format numbers in any
* locale, including support for Western, Arabic, and Indic digits. It also
* supports different kinds of numbers, including integers (123), fixed-point
* numbers (123.4), scientific notation (1.23E4), percentages (12%), and
* currency amounts ($123). All of these can be localized.
*
* <p>To obtain a <code>NumberFormat</code> for a specific locale, including the
* default locale, call one of <code>NumberFormat</code>'s factory methods, such
* as <code>getInstance()</code>. In general, do not call the
* <code>DecimalFormat</code> constructors directly, since the
* <code>NumberFormat</code> factory methods may return subclasses other than
* <code>DecimalFormat</code>. If you need to customize the format object, do
* something like this:
*
* <blockquote><pre>
* NumberFormat f = NumberFormat.getInstance(loc);
* if (f instanceof DecimalFormat) {
* ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
* }
* </pre></blockquote>
*
* <p>A <code>DecimalFormat</code> comprises a <em>pattern</em> and a set of
* <em>symbols</em>. The pattern may be set directly using
* <code>applyPattern()</code>, or indirectly using the API methods. The
* symbols are stored in a <code>DecimalFormatSymbols</code> object. When using
* the <code>NumberFormat</code> factory methods, the pattern and symbols are
* read from localized <code>ResourceBundle</code>s.
*
* <h4>Patterns</h4>
*
* <code>DecimalFormat</code> patterns have the following syntax:
* <blockquote><pre>
* <i>Pattern:</i>
* <i>PositivePattern</i>
* <i>PositivePattern</i> ; <i>NegativePattern</i>
* <i>PositivePattern:</i>
* <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
* <i>NegativePattern:</i>
* <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
* <i>Prefix:</i>
* any Unicode characters except \uFFFE, \uFFFF, and special characters
* <i>Suffix:</i>
* any Unicode characters except \uFFFE, \uFFFF, and special characters
* <i>Number:</i>
* <i>Integer</i> <i>Exponent<sub>opt</sub></i>
* <i>Integer</i> . <i>Fraction</i> <i>Exponent<sub>opt</sub></i>
* <i>Integer:</i>
* <i>MinimumInteger</i>
* #
* # <i>Integer</i>
* # , <i>Integer</i>
* <i>MinimumInteger:</i>
* 0
* 0 <i>MinimumInteger</i>
* 0 , <i>MinimumInteger</i>
* <i>Fraction:</i>
* <i>MinimumFraction<sub>opt</sub></i> <i>OptionalFraction<sub>opt</sub></i>
* <i>MinimumFraction:</i>
* 0 <i>MinimumFraction<sub>opt</sub></i>
* <i>OptionalFraction:</i>
* # <i>OptionalFraction<sub>opt</sub></i>
* <i>Exponent:</i>
* E <i>MinimumExponent</i>
* <i>MinimumExponent:</i>
* 0 <i>MinimumExponent<sub>opt</sub></i>
* </pre></blockquote>
*
* <p>A <code>DecimalFormat</code> pattern contains a positive and negative
* subpattern, for example, <code>"#,##0.00;(#,##0.00)"</code>. Each
* subpattern has a prefix, numeric part, and suffix. The negative subpattern
* is optional; if absent, then the positive subpattern prefixed with the
* localized minus sign (<code>'-'</code> in most locales) is used as the
* negative subpattern. That is, <code>"0.00"</code> alone is equivalent to
* <code>"0.00;-0.00"</code>. If there is an explicit negative subpattern, it
* serves only to specify the negative prefix and suffix; the number of digits,
* minimal digits, and other characteristics are all the same as the positive
* pattern. That means that <code>"#,##0.0#;(#)"</code> produces precisely
* the same behavior as <code>"#,##0.0#;(#,##0.0#)"</code>.
*
* <p>The prefixes, suffixes, and various symbols used for infinity, digits,
* thousands separators, decimal separators, etc. may be set to arbitrary
* values, and they will appear properly during formatting. However, care must
* be taken that the symbols and strings do not conflict, or parsing will be
* unreliable. For example, either the positive and negative prefixes or the
* suffixes must be distinct for <code>DecimalFormat.parse()</code> to be able
* to distinguish positive from negative values. (If they are identical, then
* <code>DecimalFormat</code> will behave as if no negative subpattern was
* specified.) Another example is that the decimal separator and thousands
* separator should be distinct characters, or parsing will be impossible.
*
* <p>The grouping separator is commonly used for thousands, but in some
* countries it separates ten-thousands. The grouping size is a constant number
* of digits between the grouping characters, such as 3 for 100,000,000 or 4 for
* 1,0000,0000. If you supply a pattern with multiple grouping characters, the
* interval between the last one and the end of the integer is the one that is
* used. So <code>"#,##,###,####"</code> == <code>"######,####"</code> ==
* <code>"##,####,####"</code>.
*
* <h4>Special Pattern Characters</h4>
*
* <p>Many characters in a pattern are taken literally; they are matched during
* parsing and output unchanged during formatting. Special characters, on the
* other hand, stand for other characters, strings, or classes of characters.
* They must be quoted, unless noted otherwise, if they are to appear in the
* prefix or suffix as literals.
*
* <p>The characters listed here are used in non-localized patterns. Localized
* patterns use the corresponding characters taken from this formatter's
* <code>DecimalFormatSymbols</code> object instead, and these characters lose
* their special status. Two exceptions are the currency sign and quote, which
* are not localized.
*
* <blockquote>
* <table border=0 cellspacing=3 cellpadding=0 summary="Chart showing symbol,
* location, localized, and meaning.">
* <tr bgcolor="#ccccff">
* <th align=left>Symbol
* <th align=left>Location
* <th align=left>Localized?
* <th align=left>Meaning
* <tr valign=top>
* <td><code>0</code>
* <td>Number
* <td>Yes
* <td>Digit
* <tr valign=top bgcolor="#eeeeff">
* <td><code>#</code>
* <td>Number
* <td>Yes
* <td>Digit, zero shows as absent
* <tr valign=top>
* <td><code>.</code>
* <td>Number
* <td>Yes
* <td>Decimal separator or monetary decimal separator
* <tr valign=top bgcolor="#eeeeff">
* <td><code>-</code>
* <td>Number
* <td>Yes
* <td>Minus sign
* <tr valign=top>
* <td><code>,</code>
* <td>Number
* <td>Yes
* <td>Grouping separator
* <tr valign=top bgcolor="#eeeeff">
* <td><code>E</code>
* <td>Number
* <td>Yes
* <td>Separates mantissa and exponent in scientific notation.
* <em>Need not be quoted in prefix or suffix.</em>
* <tr valign=top>
* <td><code>;</code>
* <td>Subpattern boundary
* <td>Yes
* <td>Separates positive and negative subpatterns
* <tr valign=top bgcolor="#eeeeff">
* <td><code>%</code>
* <td>Prefix or suffix
* <td>Yes
* <td>Multiply by 100 and show as percentage
* <tr valign=top>
* <td><code>\u2030</code>
* <td>Prefix or suffix
* <td>Yes
* <td>Multiply by 1000 and show as per mille value
* <tr valign=top bgcolor="#eeeeff">
* <td><code>¤</code> (<code>\u00A4</code>)
* <td>Prefix or suffix
* <td>No
* <td>Currency sign, replaced by currency symbol. If
* doubled, replaced by international currency symbol.
* If present in a pattern, the monetary decimal separator
* is used instead of the decimal separator.
* <tr valign=top>
* <td><code>'</code>
* <td>Prefix or suffix
* <td>No
* <td>Used to quote special characters in a prefix or suffix,
* for example, <code>"'#'#"</code> formats 123 to
* <code>"#123"</code>. To create a single quote
* itself, use two in a row: <code>"# o''clock"</code>.
* </table>
* </blockquote>
*
* <h4>Scientific Notation</h4>
*
* <p>Numbers in scientific notation are expressed as the product of a mantissa
* and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3. The
* mantissa is often in the range 1.0 <= x < 10.0, but it need not be.
* <code>DecimalFormat</code> can be instructed to format and parse scientific
* notation <em>only via a pattern</em>; there is currently no factory method
* that creates a scientific notation format. In a pattern, the exponent
* character immediately followed by one or more digit characters indicates
* scientific notation. Example: <code>"0.###E0"</code> formats the number
* 1234 as <code>"1.234E3"</code>.
*
* <ul>
* <li>The number of digit characters after the exponent character gives the
* minimum exponent digit count. There is no maximum. Negative exponents are
* formatted using the localized minus sign, <em>not</em> the prefix and suffix
* from the pattern. This allows patterns such as <code>"0.###E0 m/s"</code>.
*
* <li>The minimum and maximum number of integer digits are interpreted
* together:
*
* <ul>
* <li>If the maximum number of integer digits is greater than their minimum number
* and greater than 1, it forces the exponent to be a multiple of the maximum
* number of integer digits, and the minimum number of integer digits to be
* interpreted as 1. The most common use of this is to generate
* <em>engineering notation</em>, in which the exponent is a multiple of three,
* e.g., <code>"##0.#####E0"</code>. Using this pattern, the number 12345
* formats to <code>"12.345E3"</code>, and 123456 formats to
* <code>"123.456E3"</code>.
*
* <li>Otherwise, the minimum number of integer digits is achieved by adjusting the
* exponent. Example: 0.00123 formatted with <code>"00.###E0"</code> yields
* <code>"12.3E-4"</code>.
* </ul>
*
* <li>The number of significant digits in the mantissa is the sum of the
* <em>minimum integer</em> and <em>maximum fraction</em> digits, and is
* unaffected by the maximum integer digits. For example, 12345 formatted with
* <code>"##0.##E0"</code> is <code>"12.3E3"</code>. To show all digits, set
* the significant digits count to zero. The number of significant digits
* does not affect parsing.
*
* <li>Exponential patterns may not contain grouping separators.
* </ul>
*
* <h4>Rounding</h4>
*
* <code>DecimalFormat</code> provides rounding modes defined in
* {@link java.math.RoundingMode} for formatting. By default, it uses
* {@link java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}.
*
* <h4>Digits</h4>
*
* For formatting, <code>DecimalFormat</code> uses the ten consecutive
* characters starting with the localized zero digit defined in the
* <code>DecimalFormatSymbols</code> object as digits. For parsing, these
* digits as well as all Unicode decimal digits, as defined by
* {@link Character#digit Character.digit}, are recognized.
*
* <h4>Special Values</h4>
*
* <p><code>NaN</code> is formatted as a string, which typically has a single character
* <code>\uFFFD</code>. This string is determined by the
* <code>DecimalFormatSymbols</code> object. This is the only value for which
* the prefixes and suffixes are not used.
*
* <p>Infinity is formatted as a string, which typically has a single character
* <code>\u221E</code>, with the positive or negative prefixes and suffixes
* applied. The infinity string is determined by the
* <code>DecimalFormatSymbols</code> object.
*
* <p>Negative zero (<code>"-0"</code>) parses to
* <ul>
* <li><code>BigDecimal(0)</code> if <code>isParseBigDecimal()</code> is
* true,
* <li><code>Long(0)</code> if <code>isParseBigDecimal()</code> is false
* and <code>isParseIntegerOnly()</code> is true,
* <li><code>Double(-0.0)</code> if both <code>isParseBigDecimal()</code>
* and <code>isParseIntegerOnly()</code> are false.
* </ul>
*
* <h4><a name="synchronization">Synchronization</a></h4>
*
* <p>
* Decimal formats are generally not synchronized.
* It is recommended to create separate format instances for each thread.
* If multiple threads access a format concurrently, it must be synchronized
* externally.
*
* <h4>Example</h4>
*
* <blockquote><pre>
* <strong>// Print out a number using the localized number, integer, currency,
* // and percent format for each locale</strong>
* Locale[] locales = NumberFormat.getAvailableLocales();
* double myNumber = -1234.56;
* NumberFormat form;
* for (int j=0; j<4; ++j) {
* System.out.println("FORMAT");
* for (int i = 0; i < locales.length; ++i) {
* if (locales[i].getCountry().length() == 0) {
* continue; // Skip language-only locales
* }
* System.out.print(locales[i].getDisplayName());
* switch (j) {
* case 0:
* form = NumberFormat.getInstance(locales[i]); break;
* case 1:
* form = NumberFormat.getIntegerInstance(locales[i]); break;
* case 2:
* form = NumberFormat.getCurrencyInstance(locales[i]); break;
* default:
* form = NumberFormat.getPercentInstance(locales[i]); break;
* }
* if (form instanceof DecimalFormat) {
* System.out.print(": " + ((DecimalFormat) form).toPattern());
* }
* System.out.print(" -> " + form.format(myNumber));
* try {
* System.out.println(" -> " + form.parse(form.format(myNumber)));
* } catch (ParseException e) {}
* }
* }
* </pre></blockquote>
*
* @see <a href="http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html">Java Tutorial</a>
* @see NumberFormat
* @see DecimalFormatSymbols
* @see ParsePosition
* @author Mark Davis
* @author Alan Liu
*/
/**
* Creates a DecimalFormat using the default pattern and symbols
* for the default locale. This is a convenient way to obtain a
* DecimalFormat when internationalization is not the main concern.
* <p>
* To obtain standard formats for a given locale, use the factory methods
* on NumberFormat such as getNumberInstance. These factories will
* return the most appropriate sub-class of NumberFormat for a given
* locale.
*
* @see java.text.NumberFormat#getInstance
* @see java.text.NumberFormat#getNumberInstance
* @see java.text.NumberFormat#getCurrencyInstance
* @see java.text.NumberFormat#getPercentInstance
*/
public DecimalFormat() {
// try to get the pattern from the cache
// Get the pattern for the default locale.
/* update cache */
}
// Always applyPattern after the symbols are set
applyPattern(pattern, false);
}
/**
* Creates a DecimalFormat using the given pattern and the symbols
* for the default locale. This is a convenient way to obtain a
* DecimalFormat when internationalization is not the main concern.
* <p>
* To obtain standard formats for a given locale, use the factory methods
* on NumberFormat such as getNumberInstance. These factories will
* return the most appropriate sub-class of NumberFormat for a given
* locale.
*
* @param pattern A non-localized pattern string.
* @exception NullPointerException if <code>pattern</code> is null
* @exception IllegalArgumentException if the given pattern is invalid.
* @see java.text.NumberFormat#getInstance
* @see java.text.NumberFormat#getNumberInstance
* @see java.text.NumberFormat#getCurrencyInstance
* @see java.text.NumberFormat#getPercentInstance
*/
// Always applyPattern after the symbols are set
applyPattern(pattern, false);
}
/**
* Creates a DecimalFormat using the given pattern and symbols.
* Use this constructor when you need to completely customize the
* behavior of the format.
* <p>
* To obtain standard formats for a given
* locale, use the factory methods on NumberFormat such as
* getInstance or getCurrencyInstance. If you need only minor adjustments
* to a standard format, you can modify the format returned by
* a NumberFormat factory method.
*
* @param pattern a non-localized pattern string
* @param symbols the set of symbols to be used
* @exception NullPointerException if any of the given arguments is null
* @exception IllegalArgumentException if the given pattern is invalid
* @see java.text.NumberFormat#getInstance
* @see java.text.NumberFormat#getNumberInstance
* @see java.text.NumberFormat#getCurrencyInstance
* @see java.text.NumberFormat#getPercentInstance
* @see java.text.DecimalFormatSymbols
*/
// Always applyPattern after the symbols are set
applyPattern(pattern, false);
}
// Overrides
/**
* Formats a number and appends the resulting text to the given string
* buffer.
* The number can be of any subclass of {@link java.lang.Number}.
* <p>
* This implementation uses the maximum precision permitted.
* @param number the number to format
* @param toAppendTo the <code>StringBuffer</code> to which the formatted
* text is to be appended
* @param pos On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @return the value passed in as <code>toAppendTo</code>
* @exception IllegalArgumentException if <code>number</code> is
* null or not an instance of <code>Number</code>.
* @exception NullPointerException if <code>toAppendTo</code> or
* <code>pos</code> is null
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.FieldPosition
*/
FieldPosition pos) {
number instanceof AtomicInteger ||
number instanceof AtomicLong ||
(number instanceof BigInteger &&
} else if (number instanceof BigDecimal) {
} else if (number instanceof BigInteger) {
} else {
throw new IllegalArgumentException("Cannot format given Object as a Number");
}
}
/**
* Formats a double to produce a string.
* @param number The double to format
* @param result where the text is to be appended
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @return The formatted number string
* @see java.text.FieldPosition
*/
}
/**
* Formats a double to produce a string.
* @param number The double to format
* @param result where the text is to be appended
* @param delegate notified of locations of sub fields
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @return The formatted number string
*/
return result;
}
/* Detecting whether a double is negative is easy with the exception of
* the value -0.0. This is a double which has a zero mantissa (and
* exponent), but a negative sign bit. It is semantically distinct from
* a zero with a positive sign bit, and this distinction is important
* to certain kinds of computations. However, it's a little tricky to
* detect, since (-0.0 == 0.0) and !(-0.0 < 0.0). How then, you may
* ask, does it behave distinctly from +0.0? Well, 1/(-0.0) ==
* -Infinity. Proper detection of -0.0 is needed to deal with the
* issues raised by bugs 4106658, 4106667, and 4147706. Liu 7/6/98.
*/
if (multiplier != 1) {
number *= multiplier;
}
if (isNegative) {
} else {
}
if (isNegative) {
} else {
}
return result;
}
if (isNegative) {
}
// at this point we are guaranteed a nonnegative finite number.
synchronized(digitList) {
int maxIntDigits = super.getMaximumIntegerDigits();
int minIntDigits = super.getMinimumIntegerDigits();
int maxFraDigits = super.getMaximumFractionDigits();
int minFraDigits = super.getMinimumFractionDigits();
}
}
/**
* Format a long to produce a string.
* @param number The long to format
* @param result where the text is to be appended
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @return The formatted number string
* @see java.text.FieldPosition
*/
}
/**
* Format a long to produce a string.
* @param number The long to format
* @param result where the text is to be appended
* @param delegate notified of locations of sub fields
* @return The formatted number string
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.FieldPosition
*/
if (isNegative) {
}
// In general, long values always represent real finite numbers, so
// we don't have to check for +/- Infinity or NaN. However, there
// is one case we have to be careful of: The multiplier can push
// a number near MIN_VALUE or MAX_VALUE outside the legal range. We
// check for this before multiplying, and if it happens we use
// BigInteger instead.
boolean useBigInteger = false;
if (multiplier != 0) {
useBigInteger = true;
}
if (cutoff < 0) {
}
}
if (useBigInteger) {
if (isNegative) {
}
}
number *= multiplier;
if (number == 0) {
isNegative = false;
} else {
if (multiplier < 0) {
isNegative = !isNegative;
}
}
synchronized(digitList) {
int maxIntDigits = super.getMaximumIntegerDigits();
int minIntDigits = super.getMinimumIntegerDigits();
int maxFraDigits = super.getMaximumFractionDigits();
int minFraDigits = super.getMinimumFractionDigits();
}
}
/**
* Formats a BigDecimal to produce a string.
* @param number The BigDecimal to format
* @param result where the text is to be appended
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @return The formatted number string
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.FieldPosition
*/
}
/**
* Formats a BigDecimal to produce a string.
* @param number The BigDecimal to format
* @param result where the text is to be appended
* @param delegate notified of locations of sub fields
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @return The formatted number string
*/
if (multiplier != 1) {
}
if (isNegative) {
}
synchronized(digitList) {
int maxIntDigits = getMaximumIntegerDigits();
int minIntDigits = getMinimumIntegerDigits();
int maxFraDigits = getMaximumFractionDigits();
int minFraDigits = getMinimumFractionDigits();
}
}
/**
* Format a BigInteger to produce a string.
* @param number The BigInteger to format
* @param result where the text is to be appended
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @return The formatted number string
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.FieldPosition
*/
}
/**
* Format a BigInteger to produce a string.
* @param number The BigInteger to format
* @param result where the text is to be appended
* @param delegate notified of locations of sub fields
* @return The formatted number string
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.FieldPosition
*/
if (multiplier != 1) {
}
if (isNegative) {
}
synchronized(digitList) {
if (formatLong) {
maxIntDigits = super.getMaximumIntegerDigits();
minIntDigits = super.getMinimumIntegerDigits();
maxFraDigits = super.getMaximumFractionDigits();
minFraDigits = super.getMinimumFractionDigits();
} else {
if (maximumDigits < 0) {
}
}
}
}
/**
* Formats an Object producing an <code>AttributedCharacterIterator</code>.
* You can use the returned <code>AttributedCharacterIterator</code>
* to build the resulting String, as well as to determine information
* about the resulting String.
* <p>
* Each attribute key of the AttributedCharacterIterator will be of type
* <code>NumberFormat.Field</code>, with the attribute value being the
* same as the attribute key.
*
* @exception NullPointerException if obj is null.
* @exception IllegalArgumentException when the Format cannot format the
* given object.
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @param obj The object to format
* @return AttributedCharacterIterator describing the formatted value.
* @since 1.4
*/
} else if (obj instanceof BigDecimal) {
} else if (obj instanceof BigInteger) {
throw new NullPointerException(
"formatToCharacterIterator must be passed non-null object");
} else {
throw new IllegalArgumentException(
"Cannot format given Object as a Number");
}
}
/**
* Complete the formatting of a finite number. On entry, the digitList must
* be filled in with the correct digits.
*/
boolean isNegative, boolean isInteger,
int maxIntDigits, int minIntDigits,
int maxFraDigits, int minFraDigits) {
// NOTE: This isn't required anymore because DigitList takes care of this.
//
// // The negative of the exponent represents the number of leading
// // zeros between the decimal and the first non-zero digit, for
// // a value < 0.1 (e.g., for 0.00123, -fExponent == 2). If this
// // is more than the maximum fraction digits, then we have an underflow
// // for the printed representation. We recognize this here and set
// // the DigitList representation to zero in this situation.
//
// if (-digitList.decimalAt >= getMaximumFractionDigits())
// {
// digitList.count = 0;
// }
char decimal = isCurrencyFormat ?
/* Per bug 4147706, DecimalFormat must respect the sign of numbers which
* format as zero. This allows sensible computations and preserves
* relations such as signum(1/x) = signum(x), where x is +Infinity or
* -Infinity. Prior to this fix, we always formatted zero values as if
* they were positive. Liu 7/6/98.
*/
}
if (isNegative) {
} else {
}
if (useExponentialNotation) {
int iFieldEnd = -1;
int fFieldStart = -1;
// Minimum integer digits are handled in exponential format by
// adjusting the exponent. For example, 0.01234 with 3 minimum
// integer digits is "123.4E-4".
// Maximum integer digits are interpreted as indicating the
// repeating range. This is useful for engineering notation, in
// which the exponent is restricted to a multiple of 3. For
// example, 0.01234 with 3 maximum integer digits is "12.34e-3".
// If maximum integer digits are > 1 and are larger than
// minimum integer digits, then minimum integer digits are
// ignored.
int repeat = maxIntDigits;
int minimumIntegerDigits = minIntDigits;
// A repeating range is defined; adjust to it as follows.
// If repeat == 3, we have 6,5,4=>3; 3,2,1=>0; 0,-1,-2=>-3;
// -3,-4,-5=>-6, etc. This takes into account that the
// exponent we have here is off by one from what we expect;
// it is for the format 0.MMMMMx10^n.
if (exponent >= 1) {
} else {
// integer division rounds towards 0
}
minimumIntegerDigits = 1;
} else {
// No repeating range is defined; use minimum integer digits.
}
// We now output a minimum number of digits, and more if there
// are more digits, up to the maximum number of digits. We
// place the decimal point after the "integer" digits, which
// are the first (decimalAt - exponent) digits.
}
// The number of integer digits is handled specially if the number
// is zero, since then there may be no digits.
if (minimumDigits < integerDigits) {
}
if (minimumDigits > totalDigits) {
}
boolean addedDecimalSeparator = false;
for (int i=0; i<totalDigits; ++i) {
if (i == integerDigits) {
// Record field information for caller.
addedDecimalSeparator = true;
// Record field information for caller.
}
zero);
}
// Record field information for caller.
addedDecimalSeparator = true;
// Record field information for caller.
}
// Record field information
if (iFieldEnd == -1) {
}
if (addedDecimalSeparator) {
}
if (fFieldStart == -1) {
}
// The exponent is output using the pattern-specified minimum
// exponent digits. There is no maximum limit to the exponent
// digits, since truncating the exponent would result in an
// unacceptable inaccuracy.
// For zero values, we force the exponent to zero. We
// must do this here, and not earlier, because the value
// is used to determine integer digit count above.
exponent = 0;
}
if (negativeExponent) {
}
}
}
} else {
// Output the integer portion. Here 'count' is the total
// number of integer digits we will display, including both
// leading zeros required to satisfy getMinimumIntegerDigits,
// and actual digits present in the number.
int count = minIntDigits;
}
// Handle the case where getMaximumIntegerDigits() is smaller
// than the real number of integer digits. If this is so, we
// output the least significant max integer digits. For example,
// the value 1997 printed with 2 max integer digits is just "97".
if (count > maxIntDigits) {
}
// Output a real digit
} else {
// Output a leading zero
}
// Output grouping separator if necessary. Don't output a
// grouping separator if i==0 though; that's at the end of
// the integer part.
(i % groupingSize == 0)) {
}
}
// Determine whether or not there are any printable fractional
// digits. If we've used up the digits we know there aren't.
// If there is no fraction present, and we haven't printed any
// integer digits, then print a zero. Otherwise we won't print
// _any_ digits, and we won't be able to parse this string.
}
// Output the decimal separator if we always do so.
if (decimalSeparatorAlwaysShown || fractionPresent) {
}
}
for (int i=0; i < maxFraDigits; ++i) {
// Here is where we escape from the loop. We escape if we've
// output the maximum fraction digits (specified in the for
// expression above).
// We also stop when we've output the minimum digits and either:
// we have an integer, so there is no fractional stuff to
// display, or we're out of significant digits.
if (i >= minFraDigits &&
break;
}
// Output leading fractional zeros. These are zeros that come
// after the decimal but before any significant digits. These
// are only output if abs(number being formatted) < 1.0.
continue;
}
// Output a digit, if we have any precision left, or a
// zero if we don't. We don't want to output noise digits.
} else {
}
}
// Record field information for caller.
}
if (isNegative) {
}
else {
}
return result;
}
/**
* Appends the String <code>string</code> to <code>result</code>.
* <code>delegate</code> is notified of all the
* <code>FieldPosition</code>s in <code>positions</code>.
* <p>
* If one of the <code>FieldPosition</code>s in <code>positions</code>
* identifies a <code>SIGN</code> attribute, it is mapped to
* <code>signAttribute</code>. This is used
* to map the <code>SIGN</code> attribute to the <code>EXPONENT</code>
* attribute as necessary.
* <p>
*/
counter++) {
}
}
}
}
/**
* Parses text from a string to produce a <code>Number</code>.
* <p>
* The method attempts to parse text starting at the index given by
* <code>pos</code>.
* If parsing succeeds, then the index of <code>pos</code> is updated
* to the index after the last character used (parsing does not necessarily
* use all characters up to the end of the string), and the parsed
* number is returned. The updated <code>pos</code> can be used to
* indicate the starting point for the next call to this method.
* If an error occurs, then the index of <code>pos</code> is not
* changed, the error index of <code>pos</code> is set to the index of
* the character where the error occurred, and null is returned.
* <p>
* The subclass returned depends on the value of {@link #isParseBigDecimal}
* as well as on the string being parsed.
* <ul>
* <li>If <code>isParseBigDecimal()</code> is false (the default),
* most integer values are returned as <code>Long</code>
* objects, no matter how they are written: <code>"17"</code> and
* <code>"17.000"</code> both parse to <code>Long(17)</code>.
* Values that cannot fit into a <code>Long</code> are returned as
* <code>Double</code>s. This includes values with a fractional part,
* infinite values, <code>NaN</code>, and the value -0.0.
* <code>DecimalFormat</code> does <em>not</em> decide whether to
* return a <code>Double</code> or a <code>Long</code> based on the
* presence of a decimal separator in the source string. Doing so
* would prevent integers that overflow the mantissa of a double,
* such as <code>"-9,223,372,036,854,775,808.00"</code>, from being
* parsed accurately.
* <p>
* Callers may use the <code>Number</code> methods
* <code>doubleValue</code>, <code>longValue</code>, etc., to obtain
* the type they want.
* <li>If <code>isParseBigDecimal()</code> is true, values are returned
* as <code>BigDecimal</code> objects. The values are the ones
* constructed by {@link java.math.BigDecimal#BigDecimal(String)}
* for corresponding strings in locale-independent format. The
* special cases negative and positive infinity and NaN are returned
* as <code>Double</code> instances holding the values of the
* corresponding <code>Double</code> constants.
* </ul>
* <p>
* <code>DecimalFormat</code> parses all Unicode characters that represent
* decimal digits, as defined by <code>Character.digit()</code>. In
* addition, <code>DecimalFormat</code> also recognizes as digits the ten
* consecutive characters starting with the localized zero digit defined in
* the <code>DecimalFormatSymbols</code> object.
*
* @param text the string to be parsed
* @param pos A <code>ParsePosition</code> object with index and error
* index information as described above.
* @return the parsed value, or <code>null</code> if the parse fails
* @exception NullPointerException if <code>text</code> or
* <code>pos</code> is null.
*/
// special case NaN
}
boolean[] status = new boolean[STATUS_LENGTH];
return null;
}
// special case INFINITY
if (status[STATUS_INFINITE]) {
} else {
}
}
if (multiplier == 0) {
} else if (status[STATUS_POSITIVE]) {
} else {
}
}
if (isParseBigDecimal()) {
if (multiplier != 1) {
try {
}
catch (ArithmeticException e) { // non-terminating decimal expansion
}
}
if (!status[STATUS_POSITIVE]) {
}
return bigDecimalResult;
} else {
boolean gotDouble = true;
boolean gotLongMinimum = false;
double doubleResult = 0.0;
long longResult = 0;
// Finally, have DigitList parse the digits into a value.
gotDouble = false;
gotLongMinimum = true;
}
} else {
}
// Divide by multiplier. We have to be careful here not to do
// unneeded conversions between double and long.
if (multiplier != 1) {
if (gotDouble) {
} else {
// Avoid converting to double if we can
longResult /= multiplier;
} else {
gotDouble = true;
}
}
}
longResult = -longResult;
}
// At this point, if we divided the result by the multiplier, the
// result may fit into a long. We check for this case and return
// a long if possible.
// We must do this AFTER applying the negative (if appropriate)
// in order to handle the case of LONG_MIN; otherwise, if we do
// this with a positive value -LONG_MIN, the double is > 0, but
// the long is < 0. We also must retain a double in the case of
// -0.0, which will compare as == to a long 0 cast to a double
// (bug 4162852).
longResult = (long)doubleResult;
}
return gotDouble ?
}
}
/**
* Return a BigInteger multiplier.
*/
if (bigIntegerMultiplier == null) {
}
return bigIntegerMultiplier;
}
/**
* Return a BigDecimal multiplier.
*/
if (bigDecimalMultiplier == null) {
}
return bigDecimalMultiplier;
}
/**
* Parse the given text into a number. The text is parsed beginning at
* parsePosition, until an unparseable character is seen.
* @param text The string to parse.
* @param parsePosition The position at which to being parsing. Upon
* return, the first unparseable character.
* @param digits The DigitList to set to the parsed value.
* @param isExponent If true, parse an exponent. This means no
* infinite values and integer only.
* @param status Upon return contains boolean status flags indicating
* whether the value was infinite and whether it was positive.
*/
boolean status[]) {
int backup;
boolean gotPositive, gotNegative;
// check for positivePrefix; take longest
positivePrefix.length());
negativePrefix.length());
if (gotPositive && gotNegative) {
gotNegative = false;
gotPositive = false;
}
}
if (gotPositive) {
} else if (gotNegative) {
} else {
return false;
}
// process digits or Inf, find decimal position
status[STATUS_INFINITE] = false;
status[STATUS_INFINITE] = true;
} else {
// We now have a string of digits, possibly with grouping symbols,
// and decimal points. We want to process these into a DigitList.
// We don't want to put a bunch of leading zeros into the DigitList
// though, so we keep track of the location of the decimal point,
// put only significant digits into the DigitList, and adjust the
// exponent as needed.
char decimal = isCurrencyFormat ?
boolean sawDecimal = false;
boolean sawExponent = false;
boolean sawDigit = false;
// We have to track digitCount ourselves, because digits.count will
// pin when the maximum allowable digits is reached.
int digitCount = 0;
backup = -1;
/* We recognize all digit ranges, not only the Latin digit range
* '0'..'9'. We do so by using the Character.digit() method,
* which converts a valid Unicode digit to the range 0..9.
*
* The character 'ch' may be a digit. If so, place its value
* from 0 to 9 in 'digit'. First try using the locale digit,
* which may or MAY NOT be a standard Unicode digit range. If
* this fails, try using the standard Unicode digit ranges by
* calling Character.digit(). If this also fails, digit will
* have a value outside the range 0..9.
*/
}
if (digit == 0) {
// Cancel out backup setting (see grouping handler below)
sawDigit = true;
// Handle leading zeros
// Ignore leading zeros in integer part of number.
if (!sawDecimal) {
continue;
}
// If we have seen the decimal, but no significant
// digits yet, then we account for leading zeros by
// decrementing the digits.decimalAt into negative
// values.
} else {
++digitCount;
}
sawDigit = true;
++digitCount;
// Cancel out backup setting (see grouping handler below)
backup = -1;
// If we're only parsing integers, or if we ALREADY saw the
// decimal, then don't parse this one.
if (isParseIntegerOnly() || sawDecimal) {
break;
}
sawDecimal = true;
if (sawDecimal) {
break;
}
// Ignore grouping characters, if we are using them, but
// require that they be followed by a digit. Otherwise
// we backup and reprocess them.
&& !sawExponent) {
// Process the exponent by recursively calling this method.
boolean[] stat = new boolean[STATUS_LENGTH];
if (subparse(text, pos, "", Character.toString(symbols.getMinusSign()), exponentDigits, true, stat) &&
if (!stat[STATUS_POSITIVE]) {
}
sawExponent = true;
}
break; // Whether we fail or succeed, we exit this loop
}
else {
break;
}
}
if (backup != -1) {
}
// If there was no decimal point we have an integer
if (!sawDecimal) {
}
// Adjust for exponent, if any
// If none of the text string was recognized. For example, parse
// "x" with pattern "#0.00" (return index and error index both 0)
// parse "$" with pattern "$#0.00". (return index 0 and error
// index 1).
return false;
}
}
// check for suffix
if (!isExponent) {
if (gotPositive) {
positiveSuffix.length());
}
if (gotNegative) {
negativeSuffix.length());
}
// if both match, take longest
if (gotPositive && gotNegative) {
gotNegative = false;
gotPositive = false;
}
}
// fail if neither or both
if (gotPositive == gotNegative) {
return false;
}
} else {
}
return false;
}
return true;
}
/**
* Returns a copy of the decimal format symbols, which is generally not
* changed by the programmer or user.
* @return a copy of the desired DecimalFormatSymbols
* @see java.text.DecimalFormatSymbols
*/
try {
// don't allow multiple references
return null; // should never happen
}
}
/**
* Sets the decimal format symbols, which is generally not changed
* by the programmer or user.
* @param newSymbols desired DecimalFormatSymbols
* @see java.text.DecimalFormatSymbols
*/
try {
// don't allow multiple references
// should never happen
}
}
/**
* Get the positive prefix.
* <P>Examples: +123, $123, sFr123
*/
return positivePrefix;
}
/**
* Set the positive prefix.
* <P>Examples: +123, $123, sFr123
*/
}
/**
* Returns the FieldPositions of the fields in the prefix used for
* positive numbers. This is not used if the user has explicitly set
* a positive prefix via <code>setPositivePrefix</code>. This is
* lazily created.
*
* @return FieldPositions in positive prefix
*/
if (positivePrefixFieldPositions == null) {
if (posPrefixPattern != null) {
}
else {
}
}
return positivePrefixFieldPositions;
}
/**
* Get the negative prefix.
* <P>Examples: -123, ($123) (with negative suffix), sFr-123
*/
return negativePrefix;
}
/**
* Set the negative prefix.
* <P>Examples: -123, ($123) (with negative suffix), sFr-123
*/
}
/**
* Returns the FieldPositions of the fields in the prefix used for
* negative numbers. This is not used if the user has explicitly set
* a negative prefix via <code>setNegativePrefix</code>. This is
* lazily created.
*
* @return FieldPositions in positive prefix
*/
if (negativePrefixFieldPositions == null) {
if (negPrefixPattern != null) {
}
else {
}
}
return negativePrefixFieldPositions;
}
/**
* Get the positive suffix.
* <P>Example: 123%
*/
return positiveSuffix;
}
/**
* Set the positive suffix.
* <P>Example: 123%
*/
}
/**
* Returns the FieldPositions of the fields in the suffix used for
* positive numbers. This is not used if the user has explicitly set
* a positive suffix via <code>setPositiveSuffix</code>. This is
* lazily created.
*
* @return FieldPositions in positive prefix
*/
if (positiveSuffixFieldPositions == null) {
if (posSuffixPattern != null) {
}
else {
}
}
return positiveSuffixFieldPositions;
}
/**
* Get the negative suffix.
* <P>Examples: -123%, ($123) (with positive suffixes)
*/
return negativeSuffix;
}
/**
* Set the negative suffix.
* <P>Examples: 123%
*/
}
/**
* Returns the FieldPositions of the fields in the suffix used for
* negative numbers. This is not used if the user has explicitly set
* a negative suffix via <code>setNegativeSuffix</code>. This is
* lazily created.
*
* @return FieldPositions in positive prefix
*/
if (negativeSuffixFieldPositions == null) {
if (negSuffixPattern != null) {
}
else {
}
}
return negativeSuffixFieldPositions;
}
/**
* Gets the multiplier for use in percent, per mille, and similar
* formats.
*
* @see #setMultiplier(int)
*/
public int getMultiplier () {
return multiplier;
}
/**
* Sets the multiplier for use in percent, per mille, and similar
* formats.
* For a percent format, set the multiplier to 100 and the suffixes to
* have '%' (for Arabic, use the Arabic percent sign).
* For a per mille format, set the multiplier to 1000 and the suffixes to
* have '\u2030'.
*
* <P>Example: with multiplier 100, 1.23 is formatted as "123", and
* "123" is parsed into 1.23.
*
* @see #getMultiplier
*/
}
/**
* Return the grouping size. Grouping size is the number of digits between
* grouping separators in the integer portion of a number. For example,
* in the number "123,456.78", the grouping size is 3.
* @see #setGroupingSize
* @see java.text.NumberFormat#isGroupingUsed
* @see java.text.DecimalFormatSymbols#getGroupingSeparator
*/
public int getGroupingSize () {
return groupingSize;
}
/**
* Set the grouping size. Grouping size is the number of digits between
* grouping separators in the integer portion of a number. For example,
* in the number "123,456.78", the grouping size is 3.
* <br>
* The value passed in is converted to a byte, which may lose information.
* @see #getGroupingSize
* @see java.text.NumberFormat#setGroupingUsed
* @see java.text.DecimalFormatSymbols#setGroupingSeparator
*/
groupingSize = (byte)newValue;
}
/**
* Allows you to get the behavior of the decimal separator with integers.
* (The decimal separator will always appear with decimals.)
* <P>Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
*/
public boolean isDecimalSeparatorAlwaysShown() {
return decimalSeparatorAlwaysShown;
}
/**
* Allows you to set the behavior of the decimal separator with integers.
* (The decimal separator will always appear with decimals.)
* <P>Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
*/
}
/**
* Returns whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
* method returns <code>BigDecimal</code>. The default value is false.
* @see #setParseBigDecimal
* @since 1.5
*/
public boolean isParseBigDecimal() {
return parseBigDecimal;
}
/**
* Sets whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
* method returns <code>BigDecimal</code>.
* @see #isParseBigDecimal
* @since 1.5
*/
}
/**
* Standard override; no change in semantics.
*/
try {
return other;
} catch (Exception e) {
throw new InternalError();
}
}
/**
* Overrides equals
*/
{
|| (posPrefixPattern != null &&
|| (posSuffixPattern != null &&
|| (negPrefixPattern != null &&
|| (negSuffixPattern != null &&
&& (!useExponentialNotation ||
}
/**
* Overrides hashCode
*/
public int hashCode() {
// just enough fields for a reasonable distribution
}
/**
* Synthesizes a pattern string that represents the current state
* of this Format object.
* @see #applyPattern
*/
return toPattern( false );
}
/**
* Synthesizes a localized pattern string that represents the current
* state of this Format object.
* @see #applyPattern
*/
return toPattern( true );
}
/**
* Expand the affix pattern strings into the expanded affix strings. If any
* affix pattern string is null, do not expand it. This method should be
* called any time the symbols or the affix patterns change in order to keep
* the expanded affix strings up to date.
*/
private void expandAffixes() {
// Reuse one StringBuffer for better performance
if (posPrefixPattern != null) {
}
if (posSuffixPattern != null) {
}
if (negPrefixPattern != null) {
}
if (negSuffixPattern != null) {
}
}
/**
* Expand an affix pattern into an affix string. All characters in the
* pattern are literal unless prefixed by QUOTE. The following characters
* after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
* PATTERN_MINUS, and CURRENCY_SIGN. If CURRENCY_SIGN is doubled (QUOTE +
* CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
* currency code. Any other character after a QUOTE represents itself.
* QUOTE must be followed by another character; QUOTE may not occur by
* itself at the end of the pattern.
*
* @param pattern the non-null, possibly empty pattern
* @param buffer a scratch StringBuffer; its contents will be lost
* @return the expanded equivalent of pattern
*/
if (c == QUOTE) {
switch (c) {
case CURRENCY_SIGN:
++i;
} else {
}
continue;
case PATTERN_PERCENT:
c = symbols.getPercent();
break;
case PATTERN_PER_MILLE:
c = symbols.getPerMill();
break;
case PATTERN_MINUS:
c = symbols.getMinusSign();
break;
}
}
}
}
/**
* Expand an affix pattern into an array of FieldPositions describing
* how the pattern would be expanded.
* All characters in the
* pattern are literal unless prefixed by QUOTE. The following characters
* after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
* PATTERN_MINUS, and CURRENCY_SIGN. If CURRENCY_SIGN is doubled (QUOTE +
* CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
* currency code. Any other character after a QUOTE represents itself.
* QUOTE must be followed by another character; QUOTE may not occur by
* itself at the end of the pattern.
*
* @param pattern the non-null, possibly empty pattern
* @return FieldPosition array of the resulting fields.
*/
int stringIndex = 0;
if (c == QUOTE) {
int field = -1;
switch (c) {
case CURRENCY_SIGN:
++i;
} else {
}
}
}
continue;
case PATTERN_PERCENT:
c = symbols.getPercent();
field = -1;
break;
case PATTERN_PER_MILLE:
c = symbols.getPerMill();
field = -1;
break;
case PATTERN_MINUS:
c = symbols.getMinusSign();
field = -1;
break;
}
}
}
}
stringIndex++;
}
}
return EmptyFieldPositionArray;
}
/**
* Appends an affix pattern to the given StringBuffer, quoting special
* characters as needed. Uses the internal affix pattern, if that exists,
* or the literal affix, if the internal affix pattern is null. The
* appended string will generate the same affix pattern (or literal affix)
* when passed to toPattern().
*
* @param buffer the affix string is appended to this
* @param affixPattern a pattern such as posPrefixPattern; may be null
* @param expAffix a corresponding expanded affix, such as positivePrefix.
* Ignored unless affixPattern is null. If affixPattern is null, then
* expAffix is appended as a literal affix.
* @param localized true if the appended pattern should contain localized
* pattern characters; otherwise, non-localized pattern chars are appended
*/
if (affixPattern == null) {
} else {
int i;
if (i < 0) {
break;
}
if (i > pos) {
}
char c = affixPattern.charAt(++i);
++i;
if (c == QUOTE) {
// Fall through and append another QUOTE below
} else if (c == CURRENCY_SIGN &&
i<affixPattern.length() &&
++i;
// Fall through and append another CURRENCY_SIGN below
} else if (localized) {
switch (c) {
case PATTERN_PERCENT:
c = symbols.getPercent();
break;
case PATTERN_PER_MILLE:
c = symbols.getPerMill();
break;
case PATTERN_MINUS:
c = symbols.getMinusSign();
break;
}
}
}
}
}
/**
* Append an affix to the given StringBuffer, using quotes if
* there are special characters. Single quotes themselves must be
* escaped in either case.
*/
boolean needQuote;
if (localized) {
}
else {
}
else {
}
}
}
/**
* Does the real work of generating a pattern. */
for (int j = 1; j >= 0; --j) {
if (j == 1)
int i;
for (i = digitCount; i > 0; --i) {
i % groupingSize == 0) {
}
}
for (i = 0; i < getMaximumFractionDigits(); ++i) {
if (i < getMinimumFractionDigits()) {
} else {
}
}
{
for (i=0; i<minExponentDigits; ++i)
}
if (j == 1) {
|| (negSuffixPattern != null &&
break;
}
}
}
/**
* Apply the given pattern to this Format object. A pattern is a
* short-hand specification for the various formatting properties.
* These properties can also be changed individually through the
* various setter methods.
* <p>
* There is no limit to integer digits set
* by this routine, since that is the typical end-user desire;
* use setMaximumInteger if you want to set a real value.
* For negative numbers, use a second pattern, separated by a semicolon
* <P>Example <code>"#,#00.0#"</code> -> 1,234.56
* <P>This means a minimum of 2 integer digits, 1 fraction digit, and
* a maximum of 2 fraction digits.
* <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
* parentheses.
* <p>In negative patterns, the minimum and maximum counts are ignored;
* these are presumed to be set in the positive pattern.
*
* @exception NullPointerException if <code>pattern</code> is null
* @exception IllegalArgumentException if the given pattern is invalid.
*/
applyPattern(pattern, false);
}
/**
* Apply the given pattern to this Format object. The pattern
* is assumed to be in a localized notation. A pattern is a
* short-hand specification for the various formatting properties.
* These properties can also be changed individually through the
* various setter methods.
* <p>
* There is no limit to integer digits set
* by this routine, since that is the typical end-user desire;
* use setMaximumInteger if you want to set a real value.
* For negative numbers, use a second pattern, separated by a semicolon
* <P>Example <code>"#,#00.0#"</code> -> 1,234.56
* <P>This means a minimum of 2 integer digits, 1 fraction digit, and
* a maximum of 2 fraction digits.
* <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
* parentheses.
* <p>In negative patterns, the minimum and maximum counts are ignored;
* these are presumed to be set in the positive pattern.
*
* @exception NullPointerException if <code>pattern</code> is null
* @exception IllegalArgumentException if the given pattern is invalid.
*/
applyPattern(pattern, true);
}
/**
* Does the real work of applying a pattern.
*/
char zeroDigit = PATTERN_ZERO_DIGIT;
char percent = PATTERN_PERCENT;
char perMill = PATTERN_PER_MILLE;
char digit = PATTERN_DIGIT;
char separator = PATTERN_SEPARATOR;
char minus = PATTERN_MINUS;
if (localized) {
}
boolean gotNegative = false;
decimalSeparatorAlwaysShown = false;
isCurrencyFormat = false;
useExponentialNotation = false;
// Two variables are used to record the subrange of the pattern
// occupied by phase 1. This is used during the processing of the
// second pattern (the one representing negative numbers) to ensure
// that no deviation exists in phase 1 between the two patterns.
int phaseOneStart = 0;
int phaseOneLength = 0;
int start = 0;
boolean inQuote = false;
int decimalPos = -1;
int multiplier = 1;
byte groupingCount = -1;
// The phase ranges from 0 to 2. Phase 0 is the prefix. Phase 1 is
// the section of the pattern with digits, decimal separator,
// grouping characters. Phase 2 is the suffix. In phases 0 and 2,
// percent, per mille, and currency symbols are recognized and
// translated. The separation of the characters into phases is
// strictly enforced; if phase 1 characters are to appear in the
// suffix, for example, they must be quoted.
int phase = 0;
// The affix is either the prefix or the suffix.
switch (phase) {
case 0:
case 2:
// Process the prefix / suffix characters
if (inQuote) {
// A quote within quotes indicates either the closing
// quote or two quotes, which is a quote literal. That
// is, we have the second quote in 'do' or 'don''t'.
++pos;
} else {
inQuote = false; // 'do'
}
continue;
}
} else {
// Process unquoted characters seen in prefix or suffix
// phase.
ch == groupingSeparator ||
ch == decimalSeparator) {
phase = 1;
if (j == 1) {
phaseOneStart = pos;
}
--pos; // Reprocess this character
continue;
} else if (ch == CURRENCY_SIGN) {
// Use lookahead to determine if the currency sign
// is doubled or not.
if (doubled) { // Skip over the doubled character
++pos;
}
isCurrencyFormat = true;
continue;
// A quote outside quotes indicates either the
// opening quote or two quotes, which is a quote
// literal. That is, we have the first quote in 'do'
// or o''clock.
++pos;
} else {
inQuote = true; // 'do'
}
continue;
}
// Don't allow separators before we see digit
// characters of phase 1, and don't allow separators
// in the second pattern (j == 0).
throw new IllegalArgumentException("Unquoted special character '" +
}
continue;
}
// Next handle characters which are appended directly.
if (multiplier != 1) {
throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
pattern + '"');
}
multiplier = 100;
continue;
if (multiplier != 1) {
throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
pattern + '"');
}
multiplier = 1000;
continue;
continue;
}
}
// Note that if we are within quotes, or if this is an
// unquoted, non-special character, then we usually fall
// through to here.
break;
case 1:
// Phase one must be identical in the two sub-patterns. We
// enforce this by doing a direct comparison. While
// processing the first sub-pattern, we just record its
// length. While processing the second, we compare
// characters.
if (j == 1) {
} else {
if (--phaseOneLength == 0) {
phase = 2;
}
continue;
}
// Process the digits, decimal, and grouping characters. We
// record five pieces of information. We expect the digits
// to occur in the pattern ####0000.####, and we record the
// number of left digits, zero (central) digits, and right
// digits. The position of the last grouping character is
// recorded (should be somewhere within the first two blocks
// of characters), as is the position of the decimal point,
// if any (should be in the zero digits). If there is no
// decimal point, then there should be no right digits.
if (zeroDigitCount > 0) {
} else {
}
}
if (digitRightCount > 0) {
throw new IllegalArgumentException("Unexpected '0' in pattern \"" +
pattern + '"');
}
}
} else if (ch == groupingSeparator) {
groupingCount = 0;
} else if (ch == decimalSeparator) {
if (decimalPos >= 0) {
throw new IllegalArgumentException("Multiple decimal separators in pattern \"" +
pattern + '"');
}
if (useExponentialNotation) {
throw new IllegalArgumentException("Multiple exponential " +
}
useExponentialNotation = true;
minExponentDigits = 0;
// Use lookahead to parse out the exponential part
// of the pattern, then jump into phase 2.
++pos;
}
minExponentDigits < 1) {
throw new IllegalArgumentException("Malformed exponential " +
}
// Transition to phase 2
phase = 2;
--pos;
continue;
} else {
phase = 2;
--pos;
continue;
}
break;
}
}
// Handle patterns with no '0' pattern character. These patterns
// are legal, but must be interpreted. "##.###" -> "#0.###".
// ".###" -> ".0##".
/* We allow patterns of the form "####" to produce a zeroDigitCount
* of zero (got that?); although this seems like it might make it
* possible for format() to produce empty strings, format() checks
* for this condition and outputs a zero digit in this situation.
* Having a zeroDigitCount of zero yields a minimum integer digits
* of zero, which allows proper round-trip patterns. That is, we
* don't want "#" to become "#0" when toPattern() is called (even
* though that's what it really is, semantically).
*/
// Handle "###.###" and "###." and ".###"
int n = decimalPos;
if (n == 0) { // Handle ".###"
++n;
}
digitRightCount = digitLeftCount - n;
digitLeftCount = n - 1;
zeroDigitCount = 1;
}
// Do syntax checking on the digits.
throw new IllegalArgumentException("Malformed pattern \"" +
pattern + '"');
}
if (j == 1) {
/* The effectiveDecimalPos is the position the decimal is at or
* would be at if there is no decimal. Note that if decimalPos<0,
* then digitTotalCount == digitLeftCount + zeroDigitCount.
*/
this.multiplier = multiplier;
} else {
gotNegative = true;
}
}
}
// If there was no negative pattern, or if the negative pattern is
// identical to the positive pattern, then prepend the minus sign to
// the positive pattern to form the negative pattern.
if (!gotNegative ||
}
}
/**
* Sets the maximum number of digits allowed in the integer portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
* 309 is used. Negative input values are replaced with 0.
* @see NumberFormat#setMaximumIntegerDigits
*/
if (minimumIntegerDigits > maximumIntegerDigits) {
}
}
/**
* Sets the minimum number of digits allowed in the integer portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
* 309 is used. Negative input values are replaced with 0.
* @see NumberFormat#setMinimumIntegerDigits
*/
if (minimumIntegerDigits > maximumIntegerDigits) {
}
}
/**
* Sets the maximum number of digits allowed in the fraction portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
* 340 is used. Negative input values are replaced with 0.
* @see NumberFormat#setMaximumFractionDigits
*/
}
}
/**
* Sets the minimum number of digits allowed in the fraction portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
* 340 is used. Negative input values are replaced with 0.
* @see NumberFormat#setMinimumFractionDigits
*/
}
}
/**
* Gets the maximum number of digits allowed in the integer portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of the return value and
* 309 is used.
* @see #setMaximumIntegerDigits
*/
public int getMaximumIntegerDigits() {
return maximumIntegerDigits;
}
/**
* Gets the minimum number of digits allowed in the integer portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of the return value and
* 309 is used.
* @see #setMinimumIntegerDigits
*/
public int getMinimumIntegerDigits() {
return minimumIntegerDigits;
}
/**
* Gets the maximum number of digits allowed in the fraction portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of the return value and
* 340 is used.
* @see #setMaximumFractionDigits
*/
public int getMaximumFractionDigits() {
return maximumFractionDigits;
}
/**
* Gets the minimum number of digits allowed in the fraction portion of a
* number.
* For formatting numbers other than <code>BigInteger</code> and
* <code>BigDecimal</code> objects, the lower of the return value and
* 340 is used.
* @see #setMinimumFractionDigits
*/
public int getMinimumFractionDigits() {
return minimumFractionDigits;
}
/**
* Gets the currency used by this decimal format when formatting
* currency values.
* The currency is obtained by calling
* {@link DecimalFormatSymbols#getCurrency DecimalFormatSymbols.getCurrency}
* on this number format's symbols.
*
* @return the currency used by this decimal format, or <code>null</code>
* @since 1.4
*/
return symbols.getCurrency();
}
/**
* Sets the currency used by this number format when formatting
* currency values. This does not update the minimum or maximum
* number of fraction digits used by the number format.
* The currency is set by calling
* {@link DecimalFormatSymbols#setCurrency DecimalFormatSymbols.setCurrency}
* on this number format's symbols.
*
* @param currency the new currency to be used by this decimal format
* @exception NullPointerException if <code>currency</code> is null
* @since 1.4
*/
if (isCurrencyFormat) {
}
}
}
/**
* Gets the {@link java.math.RoundingMode} used in this DecimalFormat.
*
* @return The <code>RoundingMode</code> used for this DecimalFormat.
* @see #setRoundingMode(RoundingMode)
* @since 1.6
*/
return roundingMode;
}
/**
* Sets the {@link java.math.RoundingMode} used in this DecimalFormat.
*
* @param roundingMode The <code>RoundingMode</code> to be used
* @see #getRoundingMode()
* @exception NullPointerException if <code>roundingMode</code> is null.
* @since 1.6
*/
if (roundingMode == null) {
throw new NullPointerException();
}
this.roundingMode = roundingMode;
}
/**
* Adjusts the minimum and maximum fraction digits to values that
* are reasonable for the currency's default fraction digits.
*/
try {
} catch (IllegalArgumentException e) {
}
}
if (digits != -1) {
int oldMinDigits = getMinimumFractionDigits();
// Common patterns are "#.##", "#.00", "#".
// Try to adjust all of them in a reasonable way.
if (oldMinDigits == getMaximumFractionDigits()) {
} else {
}
}
}
}
/**
* Reads the default serializable fields from the stream and performs
* validations and adjustments for older serialized versions. The
* validations and adjustments are:
* <ol>
* <li>
* Verify that the superclass's digit count fields correctly reflect
* the limits imposed on formatting numbers other than
* <code>BigInteger</code> and <code>BigDecimal</code> objects. These
* limits are stored in the superclass for serialization compatibility
* with older versions, while the limits for <code>BigInteger</code> and
* <code>BigDecimal</code> objects are kept in this class.
* If, in the superclass, the minimum or maximum integer digit count is
* larger than <code>DOUBLE_INTEGER_DIGITS</code> or if the minimum or
* maximum fraction digit count is larger than
* <code>DOUBLE_FRACTION_DIGITS</code>, then the stream data is invalid
* and this method throws an <code>InvalidObjectException</code>.
* <li>
* If <code>serialVersionOnStream</code> is less than 4, initialize
* <code>roundingMode</code> to {@link java.math.RoundingMode#HALF_EVEN
* RoundingMode.HALF_EVEN}. This field is new with version 4.
* <li>
* If <code>serialVersionOnStream</code> is less than 3, then call
* the setters for the minimum and maximum integer and fraction digits with
* the values of the corresponding superclass getters to initialize the
* fields in this class. The fields in this class are new with version 3.
* <li>
* If <code>serialVersionOnStream</code> is less than 1, indicating that
* the stream was written by JDK 1.1, initialize
* <code>useExponentialNotation</code>
* to false, since it was not present in JDK 1.1.
* <li>
* Set <code>serialVersionOnStream</code> to the maximum allowed value so
* that default serialization will work properly if this object is streamed
* out again.
* </ol>
*
* <p>Stream versions older than 2 will not have the affix pattern variables
* <code>posPrefixPattern</code> etc. As a result, they will be initialized
* to <code>null</code>, which means the affix strings will be taken as
* literal values. This is exactly what we want, since that corresponds to
* the pre-version-2 behavior.
*/
throws IOException, ClassNotFoundException
{
if (serialVersionOnStream < 4) {
}
// We only need to check the maximum counts because NumberFormat
// .readObject has already ensured that the maximum is greater than the
// minimum count.
if (super.getMaximumIntegerDigits() > DOUBLE_INTEGER_DIGITS ||
super.getMaximumFractionDigits() > DOUBLE_FRACTION_DIGITS) {
throw new InvalidObjectException("Digit count out of range");
}
if (serialVersionOnStream < 3) {
}
if (serialVersionOnStream < 1) {
// Didn't have exponential fields
useExponentialNotation = false;
}
}
//----------------------------------------------------------------------
// INSTANCE VARIABLES
//----------------------------------------------------------------------
/**
* The symbol used as a prefix when formatting positive numbers, e.g. "+".
*
* @serial
* @see #getPositivePrefix
*/
/**
* The symbol used as a suffix when formatting positive numbers.
* This is often an empty string.
*
* @serial
* @see #getPositiveSuffix
*/
/**
* The symbol used as a prefix when formatting negative numbers, e.g. "-".
*
* @serial
* @see #getNegativePrefix
*/
/**
* The symbol used as a suffix when formatting negative numbers.
* This is often an empty string.
*
* @serial
* @see #getNegativeSuffix
*/
/**
* The prefix pattern for non-negative numbers. This variable corresponds
* to <code>positivePrefix</code>.
*
* <p>This pattern is expanded by the method <code>expandAffix()</code> to
* <code>positivePrefix</code> to update the latter to reflect changes in
* <code>symbols</code>. If this variable is <code>null</code> then
* <code>positivePrefix</code> is taken as a literal value that does not
* change when <code>symbols</code> changes. This variable is always
* <code>null</code> for <code>DecimalFormat</code> objects older than
* stream version 2 restored from stream.
*
* @serial
* @since 1.3
*/
/**
* The suffix pattern for non-negative numbers. This variable corresponds
* to <code>positiveSuffix</code>. This variable is analogous to
* <code>posPrefixPattern</code>; see that variable for further
* documentation.
*
* @serial
* @since 1.3
*/
/**
* The prefix pattern for negative numbers. This variable corresponds
* to <code>negativePrefix</code>. This variable is analogous to
* <code>posPrefixPattern</code>; see that variable for further
* documentation.
*
* @serial
* @since 1.3
*/
/**
* The suffix pattern for negative numbers. This variable corresponds
* to <code>negativeSuffix</code>. This variable is analogous to
* <code>posPrefixPattern</code>; see that variable for further
* documentation.
*
* @serial
* @since 1.3
*/
/**
* The multiplier for use in percent, per mille, etc.
*
* @serial
* @see #getMultiplier
*/
/**
* The number of digits between grouping separators in the integer
* portion of a number. Must be greater than 0 if
* <code>NumberFormat.groupingUsed</code> is true.
*
* @serial
* @see #getGroupingSize
* @see java.text.NumberFormat#isGroupingUsed
*/
/**
* If true, forces the decimal separator to always appear in a formatted
* number, even if the fractional part of the number is zero.
*
* @serial
* @see #isDecimalSeparatorAlwaysShown
*/
private boolean decimalSeparatorAlwaysShown = false;
/**
* If true, parse returns BigDecimal wherever possible.
*
* @serial
* @see #isParseBigDecimal
* @since 1.5
*/
private boolean parseBigDecimal = false;
/**
* True if this object represents a currency format. This determines
* whether the monetary decimal separator is used instead of the normal one.
*/
private transient boolean isCurrencyFormat = false;
/**
* The <code>DecimalFormatSymbols</code> object used by this format.
* It contains the symbols used to format numbers, e.g. the grouping separator,
* decimal separator, and so on.
*
* @serial
* @see #setDecimalFormatSymbols
* @see java.text.DecimalFormatSymbols
*/
/**
* True to force the use of exponential (i.e. scientific) notation when formatting
* numbers.
*
* @serial
* @since 1.2
*/
/**
* FieldPositions describing the positive prefix String. This is
* lazily created. Use <code>getPositivePrefixFieldPositions</code>
* when needed.
*/
/**
* FieldPositions describing the positive suffix String. This is
* lazily created. Use <code>getPositiveSuffixFieldPositions</code>
* when needed.
*/
/**
* FieldPositions describing the negative prefix String. This is
* lazily created. Use <code>getNegativePrefixFieldPositions</code>
* when needed.
*/
/**
* FieldPositions describing the negative suffix String. This is
* lazily created. Use <code>getNegativeSuffixFieldPositions</code>
* when needed.
*/
/**
* The minimum number of digits used to display the exponent when a number is
* formatted in exponential notation. This field is ignored if
* <code>useExponentialNotation</code> is not true.
*
* @serial
* @since 1.2
*/
/**
* The maximum number of digits allowed in the integer portion of a
* <code>BigInteger</code> or <code>BigDecimal</code> number.
* <code>maximumIntegerDigits</code> must be greater than or equal to
* <code>minimumIntegerDigits</code>.
*
* @serial
* @see #getMaximumIntegerDigits
* @since 1.5
*/
/**
* The minimum number of digits allowed in the integer portion of a
* <code>BigInteger</code> or <code>BigDecimal</code> number.
* <code>minimumIntegerDigits</code> must be less than or equal to
* <code>maximumIntegerDigits</code>.
*
* @serial
* @see #getMinimumIntegerDigits
* @since 1.5
*/
/**
* The maximum number of digits allowed in the fractional portion of a
* <code>BigInteger</code> or <code>BigDecimal</code> number.
* <code>maximumFractionDigits</code> must be greater than or equal to
* <code>minimumFractionDigits</code>.
*
* @serial
* @see #getMaximumFractionDigits
* @since 1.5
*/
/**
* The minimum number of digits allowed in the fractional portion of a
* <code>BigInteger</code> or <code>BigDecimal</code> number.
* <code>minimumFractionDigits</code> must be less than or equal to
* <code>maximumFractionDigits</code>.
*
* @serial
* @see #getMinimumFractionDigits
* @since 1.5
*/
/**
* The {@link java.math.RoundingMode} used in this DecimalFormat.
*
* @serial
* @since 1.6
*/
//----------------------------------------------------------------------
/**
* The internal serial version which says which version was written.
* Possible values are:
* <ul>
* <li><b>0</b> (default): versions before the Java 2 platform v1.2
* <li><b>1</b>: version for 1.2, which includes the two new fields
* <code>useExponentialNotation</code> and
* <code>minExponentDigits</code>.
* <li><b>2</b>: version for 1.3 and later, which adds four new fields:
* <code>posPrefixPattern</code>, <code>posSuffixPattern</code>,
* <code>negPrefixPattern</code>, and <code>negSuffixPattern</code>.
* <li><b>3</b>: version for 1.5 and later, which adds five new fields:
* <code>maximumIntegerDigits</code>,
* <code>minimumIntegerDigits</code>,
* <code>maximumFractionDigits</code>,
* <code>minimumFractionDigits</code>, and
* <code>parseBigDecimal</code>.
* <li><b>4</b>: version for 1.6 and later, which adds one new field:
* <code>roundingMode</code>.
* </ul>
* @since 1.2
* @serial
*/
//----------------------------------------------------------------------
// CONSTANTS
//----------------------------------------------------------------------
// Constants for characters used in programmatic (unlocalized) patterns.
/**
* The CURRENCY_SIGN is the standard Unicode symbol for currency. It
* is used in patterns and substituted with either the currency symbol,
* or if it is doubled, with the international currency symbol. If the
* CURRENCY_SIGN is seen in a pattern, then the decimal separator is
* replaced with the monetary decimal separator.
*
* The CURRENCY_SIGN is not localized.
*/
// Upper limit on integer and fraction digits for a Java double
// Upper limit on integer and fraction digits for BigDecimal and BigInteger
// Proclaim JDK 1.1 serial compatibility.
/**
* Cache to hold the NumberPattern of a Locale.
*/
}