/*
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
* (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.
*
*/
package java.text;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import sun.misc.FloatingDecimal;
/**
* Digit List. Private to DecimalFormat.
* Handles the transcoding
* between numeric values and strings of characters. Only handles
* non-negative numbers. The division of labor between DigitList and
* DecimalFormat is that DigitList handles the radix 10 representation
* issues; DecimalFormat handles the locale-specific issues such as
* positive/negative, grouping, decimal point, currency, and so on.
*
* A DigitList is really a representation of a floating point value.
* It may be an integer value; we assume that a double has sufficient
* precision to represent all digits of a long.
*
* The DigitList representation consists of a string of characters,
* which are the digits radix 10, from '0' to '9'. It also has a radix
* 10 exponent associated with it. The value represented by a DigitList
* object can be computed by mulitplying the fraction f, where 0 <= f < 1,
* derived by placing all the digits of the list to the right of the
* decimal point, by 10^exponent.
*
* @see Locale
* @see Format
* @see NumberFormat
* @see DecimalFormat
* @see ChoiceFormat
* @see MessageFormat
* @author Mark Davis, Alan Liu
*/
final class DigitList implements Cloneable {
/**
* The maximum number of significant digits in an IEEE 754 double, that
* is, in a Java double. This must not be increased, or garbage digits
* will be generated, and should not be decreased, or accuracy will be lost.
*/
public static final int MAX_COUNT = 19; // == Long.toString(Long.MAX_VALUE).length()
/**
* These data members are intentionally public and can be set directly.
*
* The value represented is given by placing the decimal point before
* digits[decimalAt]. If decimalAt is < 0, then leading zeros between
* the decimal point and the first nonzero digit are implied. If decimalAt
* is > count, then trailing zeros between the digits[count-1] and the
* decimal point are implied.
*
* Equivalently, the represented value is given by f * 10^decimalAt. Here
* f is a value 0.1 <= f < 1 arrived at by placing the digits in Digits to
* the right of the decimal.
*
* DigitList is normalized, so if it is non-zero, figits[0] is non-zero. We
* don't allow denormalized numbers because our exponent is effectively of
* unlimited magnitude. The count value contains the number of significant
* digits present in digits[].
*
* Zero is represented by any DigitList with count == 0 or with each digits[i]
* for all i <= count == '0'.
*/
public int decimalAt = 0;
public int count = 0;
public char[] digits = new char[MAX_COUNT];
private char[] data;
private RoundingMode roundingMode = RoundingMode.HALF_EVEN;
private boolean isNegative = false;
/**
* Return true if the represented number is zero.
*/
boolean isZero() {
for (int i=0; i < count; ++i) {
if (digits[i] != '0') {
return false;
}
}
return true;
}
/**
* Set the rounding mode
*/
void setRoundingMode(RoundingMode r) {
roundingMode = r;
}
/**
* Clears out the digits.
* Use before appending them.
* Typically, you set a series of digits with append, then at the point
* you hit the decimal point, you set myDigitList.decimalAt = myDigitList.count;
* then go on appending digits.
*/
public void clear () {
decimalAt = 0;
count = 0;
}
/**
* Appends a digit to the list, extending the list when necessary.
*/
public void append(char digit) {
if (count == digits.length) {
char[] data = new char[count + 100];
System.arraycopy(digits, 0, data, 0, count);
digits = data;
}
digits[count++] = digit;
}
/**
* Utility routine to get the value of the digit list
* If (count == 0) this throws a NumberFormatException, which
* mimics Long.parseLong().
*/
public final double getDouble() {
if (count == 0) {
return 0.0;
}
StringBuffer temp = getStringBuffer();
temp.append('.');
temp.append(digits, 0, count);
temp.append('E');
temp.append(decimalAt);
return Double.parseDouble(temp.toString());
}
/**
* Utility routine to get the value of the digit list.
* If (count == 0) this returns 0, unlike Long.parseLong().
*/
public final long getLong() {
// for now, simple implementation; later, do proper IEEE native stuff
if (count == 0) {
return 0;
}
// We have to check for this, because this is the one NEGATIVE value
// we represent. If we tried to just pass the digits off to parseLong,
// we'd get a parse failure.
if (isLongMIN_VALUE()) {
return Long.MIN_VALUE;
}
StringBuffer temp = getStringBuffer();
temp.append(digits, 0, count);
for (int i = count; i < decimalAt; ++i) {
temp.append('0');
}
return Long.parseLong(temp.toString());
}
public final BigDecimal getBigDecimal() {
if (count == 0) {
if (decimalAt == 0) {
return BigDecimal.ZERO;
} else {
return new BigDecimal("0E" + decimalAt);
}
}
if (decimalAt == count) {
return new BigDecimal(digits, 0, count);
} else {
return new BigDecimal(digits, 0, count).scaleByPowerOfTen(decimalAt - count);
}
}
/**
* Return true if the number represented by this object can fit into
* a long.
* @param isPositive true if this number should be regarded as positive
* @param ignoreNegativeZero true if -0 should be regarded as identical to
* +0; otherwise they are considered distinct
* @return true if this number fits into a Java long
*/
boolean fitsIntoLong(boolean isPositive, boolean ignoreNegativeZero) {
// Figure out if the result will fit in a long. We have to
// first look for nonzero digits after the decimal point;
// then check the size. If the digit count is 18 or less, then
// the value can definitely be represented as a long. If it is 19
// then it may be too large.
// Trim trailing zeros. This does not change the represented value.
while (count > 0 && digits[count - 1] == '0') {
--count;
}
if (count == 0) {
// Positive zero fits into a long, but negative zero can only
// be represented as a double. - bug 4162852
return isPositive || ignoreNegativeZero;
}
if (decimalAt < count || decimalAt > MAX_COUNT) {
return false;
}
if (decimalAt < MAX_COUNT) return true;
// At this point we have decimalAt == count, and count == MAX_COUNT.
// The number will overflow if it is larger than 9223372036854775807
// or smaller than -9223372036854775808.
for (int i=0; icount-1. If 0, then all digits are rounded away, and
* this method returns true if a one should be generated (e.g., formatting
* 0.09 with "#.#").
* @param alreadyRounded whether or not rounding up has already happened.
* @param valueExactAsDecimal whether or not collected digits provide
* an exact decimal representation of the value.
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @return true if digit maximumDigits-1 should be
* incremented
*/
private boolean shouldRoundUp(int maximumDigits,
boolean alreadyRounded,
boolean valueExactAsDecimal) {
if (maximumDigits < count) {
/*
* To avoid erroneous double-rounding or truncation when converting
* a binary double value to text, information about the exactness
* of the conversion result in FloatingDecimal, as well as any
* rounding done, is needed in this class.
*
* - For the HALF_DOWN, HALF_EVEN, HALF_UP rounding rules below:
* In the case of formating float or double, We must take into
* account what FloatingDecimal has done in the binary to decimal
* conversion.
*
* Considering the tie cases, FloatingDecimal may round up the
* value (returning decimal digits equal to tie when it is below),
* or "truncate" the value to the tie while value is above it,
* or provide the exact decimal digits when the binary value can be
* converted exactly to its decimal representation given formating
* rules of FloatingDecimal ( we have thus an exact decimal
* representation of the binary value).
*
* - If the double binary value was converted exactly as a decimal
* value, then DigitList code must apply the expected rounding
* rule.
*
* - If FloatingDecimal already rounded up the decimal value,
* DigitList should neither round up the value again in any of
* the three rounding modes above.
*
* - If FloatingDecimal has truncated the decimal value to
* an ending '5' digit, DigitList should round up the value in
* all of the three rounding modes above.
*
*
* This has to be considered only if digit at maximumDigits index
* is exactly the last one in the set of digits, otherwise there are
* remaining digits after that position and we don't have to consider
* what FloatingDecimal did.
*
* - Other rounding modes are not impacted by these tie cases.
*
* - For other numbers that are always converted to exact digits
* (like BigInteger, Long, ...), the passed alreadyRounded boolean
* have to be set to false, and valueExactAsDecimal has to be set to
* true in the upper DigitList call stack, providing the right state
* for those situations..
*/
switch(roundingMode) {
case UP:
for (int i=maximumDigits; i