2362N/A * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 0N/A * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved 0N/A * The original version of this source code and documentation is copyrighted 0N/A * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 0N/A * materials are provided under terms of a License Agreement between Taligent 0N/A * and Sun. This technology is protected by multiple US and International 0N/A * patents. This notice and attribution to Taligent may not be removed. 0N/A * Taligent is a registered trademark of Taligent, Inc. 0N/A * Digit List. Private to DecimalFormat. 0N/A * Handles the transcoding 0N/A * between numeric values and strings of characters. Only handles 0N/A * non-negative numbers. The division of labor between DigitList and 0N/A * DecimalFormat is that DigitList handles the radix 10 representation 0N/A * issues; DecimalFormat handles the locale-specific issues such as 0N/A * A DigitList is really a representation of a floating point value. 0N/A * It may be an integer value; we assume that a double has sufficient 0N/A * precision to represent all digits of a long. 0N/A * The DigitList representation consists of a string of characters, 0N/A * which are the digits radix 10, from '0' to '9'. It also has a radix 0N/A * 10 exponent associated with it. The value represented by a DigitList 0N/A * object can be computed by mulitplying the fraction f, where 0 <= f < 1, 0N/A * derived by placing all the digits of the list to the right of the 0N/A * decimal point, by 10^exponent. 0N/A * @see DecimalFormat 0N/A * @see MessageFormat 0N/A * @author Mark Davis, Alan Liu 0N/A * The maximum number of significant digits in an IEEE 754 double, that 0N/A * is, in a Java double. This must not be increased, or garbage digits 0N/A * will be generated, and should not be decreased, or accuracy will be lost. 0N/A public static final int MAX_COUNT =
19;
// == Long.toString(Long.MAX_VALUE).length() 0N/A * These data members are intentionally public and can be set directly. 0N/A * The value represented is given by placing the decimal point before 0N/A * digits[decimalAt]. If decimalAt is < 0, then leading zeros between 0N/A * the decimal point and the first nonzero digit are implied. If decimalAt 0N/A * is > count, then trailing zeros between the digits[count-1] and the 0N/A * decimal point are implied. 0N/A * Equivalently, the represented value is given by f * 10^decimalAt. Here 0N/A * f is a value 0.1 <= f < 1 arrived at by placing the digits in Digits to 0N/A * the right of the decimal. 0N/A * DigitList is normalized, so if it is non-zero, figits[0] is non-zero. We 0N/A * don't allow denormalized numbers because our exponent is effectively of 0N/A * unlimited magnitude. The count value contains the number of significant 0N/A * digits present in digits[]. 0N/A * Zero is represented by any DigitList with count == 0 or with each digits[i] 0N/A * for all i <= count == '0'. 0N/A * Return true if the represented number is zero. 0N/A * Set the rounding mode 0N/A * Clears out the digits. 0N/A * Use before appending them. 0N/A * Typically, you set a series of digits with append, then at the point 0N/A * you hit the decimal point, you set myDigitList.decimalAt = myDigitList.count; 0N/A * then go on appending digits. 0N/A * Appends a digit to the list, extending the list when necessary. 0N/A * Utility routine to get the value of the digit list 0N/A * If (count == 0) this throws a NumberFormatException, which 0N/A * mimics Long.parseLong(). 0N/A * Utility routine to get the value of the digit list. 0N/A * If (count == 0) this returns 0, unlike Long.parseLong(). 0N/A // for now, simple implementation; later, do proper IEEE native stuff 0N/A // We have to check for this, because this is the one NEGATIVE value 0N/A // we represent. If we tried to just pass the digits off to parseLong, 0N/A // we'd get a parse failure. 0N/A * Return true if the number represented by this object can fit into 0N/A * @param isPositive true if this number should be regarded as positive 0N/A * @param ignoreNegativeZero true if -0 should be regarded as identical to 0N/A * +0; otherwise they are considered distinct 0N/A * @return true if this number fits into a Java long 0N/A // Figure out if the result will fit in a long. We have to 0N/A // first look for nonzero digits after the decimal point; 0N/A // then check the size. If the digit count is 18 or less, then 0N/A // the value can definitely be represented as a long. If it is 19 0N/A // then it may be too large. 0N/A // Trim trailing zeros. This does not change the represented value. 0N/A // Positive zero fits into a long, but negative zero can only 0N/A // be represented as a double. - bug 4162852 0N/A // At this point we have decimalAt == count, and count == MAX_COUNT. 0N/A // The number will overflow if it is larger than 9223372036854775807 0N/A // or smaller than -9223372036854775808. 0N/A // At this point the first count digits match. If decimalAt is less 0N/A // than count, then the remaining digits are zero, and we return true. 0N/A // Now we have a representation of Long.MIN_VALUE, without the leading 0N/A // negative sign. If this represents a positive value, then it does 0N/A // not fit; otherwise it fits. 0N/A * Set the digit list to a representation of the given double value. 0N/A * This method supports fixed-point notation. 0N/A * @param isNegative Boolean value indicating whether the number is negative. 0N/A * @param source Value to be converted; must not be Inf, -Inf, Nan, 0N/A * @param maximumFractionDigits The most fractional digits which should 0N/A * Set the digit list to a representation of the given double value. 0N/A * This method supports both fixed-point and exponential notation. 0N/A * @param isNegative Boolean value indicating whether the number is negative. 0N/A * @param source Value to be converted; must not be Inf, -Inf, Nan, 0N/A * @param maximumDigits The most fractional or total digits which should 0N/A * @param fixedPoint If true, then maximumDigits is the maximum 0N/A * fractional digits to be converted. If false, total digits. 0N/A * Generate a representation of the form DDDDD, DDDDD.DDDDD, or 0N/A // Number of zeros between decimal point and first non-zero digit after 0N/A // decimal point, for numbers < 1. 0N/A }
else if (c ==
'e' || c ==
'E') {
0N/A // The negative of the exponent represents the number of leading 0N/A // zeros between the decimal and the first non-zero digit, for 0N/A // a value < 0.1 (e.g., for 0.00123, -decimalAt == 2). If this 0N/A // is more than the maximum fraction digits, then we have an underflow 0N/A // for the printed representation. 0N/A // Handle an underflow to zero when we round something like 0N/A // 0.0009 to 2 fractional digits. 0N/A // If we round 0.0009 to 3 fractional digits, then we have to 0N/A // create a new one digit in the least significant location. 0N/A // else fall through 0N/A // Eliminate trailing zeros. 0N/A // Eliminate digits beyond maximum digits to be displayed. 0N/A // Round up if appropriate. 0N/A * Round the representation to the given number of digits. 0N/A * @param maximumDigits The maximum number of digits to be shown. 0N/A * Upon return, count will be less than or equal to maximumDigits. 0N/A // Eliminate digits beyond maximum digits to be displayed. 0N/A // Round up if appropriate. 0N/A // Rounding up involved incrementing digits from LSD to MSD. 0N/A // In most cases this is simple, but in a worst case situation 0N/A // (9999..99) we have to adjust the decimalAt value. 0N/A // We have all 9's, so we increment to a single digit 0N/A // of one and adjust the exponent. 0N/A // digits[maximumDigits] = '0'; // Unnecessary since we'll truncate this 0N/A // Eliminate trailing zeros. 0N/A * Return true if truncating the representation to the given number 0N/A * of digits will result in an increment to the last digit. This 0N/A * method implements the rounding modes defined in the 0N/A * java.math.RoundingMode class. 0N/A * @param maximumDigits the number of digits to keep, from 0 to 0N/A * <code>count-1</code>. If 0, then all digits are rounded away, and 0N/A * this method returns true if a one should be generated (e.g., formatting 0N/A * @exception ArithmeticException if rounding is needed with rounding 0N/A * mode being set to RoundingMode.UNNECESSARY 0N/A * @return true if digit <code>maximumDigits-1</code> should be 0N/A // Implement IEEE half-even rounding 0N/A "Rounding needed with the rounding mode being set to RoundingMode.UNNECESSARY");
0N/A * Utility routine to set the value of the digit list from a long 0N/A * Set the digit list to a representation of the given long value. 0N/A * @param isNegative Boolean value indicating whether the number is negative. 0N/A * @param source Value to be converted; must be >= 0 or == 0N/A * @param maximumDigits The most digits which should be converted. 0N/A * If maximumDigits is lower than the number of significant digits 0N/A * in source, the representation will be rounded. Ignored if <= 0. 0N/A // This method does not expect a negative number. However, 0N/A // "source" can be a Long.MIN_VALUE (-9223372036854775808), 0N/A // if the number being formatted is a Long.MIN_VALUE. In that 0N/A // case, it will be formatted as -Long.MIN_VALUE, a number 0N/A // which is outside the legal range of a long, but which can 0N/A // be represented by DigitList. 0N/A // Rewritten to improve performance. I used to call 0N/A // Long.toString(), which was about 4x slower than this code. 0N/A // Don't copy trailing zeros. We are guaranteed that there is at 0N/A // least one non-zero digit, so we don't have to check lower bounds. 0N/A * Set the digit list to a representation of the given BigDecimal value. 0N/A * This method supports both fixed-point and exponential notation. 0N/A * @param isNegative Boolean value indicating whether the number is negative. 0N/A * @param source Value to be converted; must not be a value <= 0. 0N/A * @param maximumDigits The most fractional or total digits which should 0N/A * @param fixedPoint If true, then maximumDigits is the maximum 0N/A * fractional digits to be converted. If false, total digits. 0N/A * Set the digit list to a representation of the given BigInteger value. 0N/A * @param isNegative Boolean value indicating whether the number is negative. 0N/A * @param source Value to be converted; must be >= 0. 0N/A * @param maximumDigits The most digits which should be converted. 0N/A * If maximumDigits is lower than the number of significant digits 0N/A * in source, the representation will be rounded. Ignored if <= 0. 0N/A * equality test between two digit lists. 0N/A if (
this ==
obj)
// quick check 0N/A * Generates the hash code for the digit list. 0N/A * Creates a copy of this object. 0N/A * @return a clone of this instance. 0N/A * Returns true if this DigitList represents Long.MIN_VALUE; 0N/A * false, otherwise. This is required so that getLong() works. 0N/A }
else if (c ==
'+') {
0N/A if (c >=
'0' && c <=
'9') {
0N/A // The digit part of -9223372036854775808L