APIdia Launch Partner
Top Description Fields Constructors Methods
java.text

pack-priv final Class DigitList

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.math.BigDecimal, .BigInteger, .RoundingMode, java.nio.charset.StandardCharsets, jdk.internal.access.SharedSecrets, jdk.internal.math.FloatingDecimal, jdk.internal.util.ArraysSupport, jdk.internal.vm.annotation.Stable

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.

Author
Mark Davis, Alan Liu
See Also
Locale, Format, NumberFormat, DecimalFormat, ChoiceFormat, MessageFormat

Field Summary

Modifier and TypeField and Description
private static final boolean
public int
public int
decimalAt

These data members are intentionally public and can be set directly.

public byte[]
private boolean
private static final byte[]
public static final int
MAX_COUNT

The maximum number of significant digits in an IEEE 754 double, that is, in a Java double.

private RoundingMode

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
append(char digit)

Appends a digit to the list, extending the list when necessary.

public void
clear()

Clears out the digits.

public Object

Returns:

a clone of this instance.
clone
()

Overrides java.lang.Object.clone.

Creates a copy of this object.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

equality test between two digit lists.

private void
extendDigits(int len)

pack-priv boolean

Returns:

true if this number fits into a Java long
fitsIntoLong
(boolean
true if this number should be regarded as positive
isPositive
,
boolean
true if -0 should be regarded as identical to +0; otherwise they are considered distinct
ignoreNegativeZero
)

Return true if the number represented by this object can fit into a long.

public BigDecimal
getBigDecimal()

Utility routine to get the value of the digit list.

public double
getDouble()

Utility routine to get the value of the digit list If (count == 0) this returns 0.0, unlike Double.parseDouble("") which throws NumberFormatException.

public long
getLong()

Utility routine to get the value of the digit list.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Generates the hash code for the digit list.

pack-priv boolean
isZero()

Return true if the represented number is zero.

private boolean
nonZeroAfterIndex(int index)

Return true if there exists a non-zero digit in the digit list from the given index until the end.

private void
round(int
The maximum number of digits to be shown.
maximumDigits
,
boolean
whether or not rounding up has already happened.
alreadyRounded
,
boolean
whether or not collected digits provide an exact decimal representation of the value. Upon return, count will be less than or equal to maximumDigits.
valueExactAsDecimal
)

Round the representation to the given number of digits.

private void
roundInt(int
The maximum number of digits to be shown. Upon return, count will be less than or equal to maximumDigits.
maximumDigits
)

Round the representation to the given number of digits.

private int

Returns:

The new maximum digits after rounding.
roundUp
(int
The maximum number of digits to be shown.
maximumDigits
)

Round the digit list up numerically.

pack-priv void
set(boolean
Boolean value indicating whether the number is negative.
isNegative
,
double
Value to be converted; must not be Inf, -Inf, Nan, or a value <= 0.
source
,
int
The most fractional digits which should be converted.
maximumFractionDigits
)

Set the digit list to a representation of the given double value.

pack-priv void
set(boolean
Boolean value indicating whether the number is negative.
isNegative
,
double
Value to be converted; must not be Inf, -Inf, Nan, or a value <= 0.
source
,
int
The most fractional or total digits which should be converted.
maximumDigits
,
boolean
If true, then maximumDigits is the maximum fractional digits to be converted. If false, total digits.
fixedPoint
)

Set the digit list to a representation of the given double value.

private void
set(boolean isNegative, int exp, boolean
whether or not rounding up has already happened.
roundedUp
,
boolean
whether or not collected digits provide an exact decimal representation of the value.
valueExactAsDecimal
,
int maximumDigits, boolean fixedPoint)

Generate a representation of the form DDDDD, DDDDD.DDDDD, or DDDDDE+/-DDDDD.

pack-priv void
set(boolean isNegative, long source)

Utility routine to set the value of the digit list from a long

pack-priv void
set(boolean
Boolean value indicating whether the number is negative.
isNegative
,
long
Value to be converted; must be >= 0 or == Long.MIN_VALUE.
source
,
int
The most digits which should be converted. If maximumDigits is lower than the number of significant digits in source, the representation will be rounded. Ignored if <= 0.
maximumDigits
)

Set the digit list to a representation of the given long value.

pack-priv void
set(boolean
Boolean value indicating whether the number is negative.
isNegative
,
BigDecimal
Value to be converted; must not be a value <= 0.
source
,
int
The most fractional or total digits which should be converted.
maximumDigits
,
boolean
If true, then maximumDigits is the maximum fractional digits to be converted. If false, total digits.
fixedPoint
)

Set the digit list to a representation of the given BigDecimal value.

pack-priv void
set(boolean
Boolean value indicating whether the number is negative.
isNegative
,
BigInteger
Value to be converted; must be >= 0.
source
,
int
The most digits which should be converted. If maximumDigits is lower than the number of significant digits in source, the representation will be rounded. Ignored if <= 0.
maximumDigits
)

Set the digit list to a representation of the given BigInteger value.

pack-priv void
setRoundingMode(RoundingMode r)

Set the rounding mode

private boolean

Returns:

true if digit maximumDigits-1 should be incremented
shouldRoundUp
(int
the number of digits to keep, from 0 to count-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 "#.#").
maximumDigits
,
boolean
whether or not rounding up has already happened.
alreadyRounded
,
boolean
whether or not collected digits provide an exact decimal representation of the value.
valueExactAsDecimal
)

Return true if truncating the representation to the given number of digits will result in an increment to the last digit.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

COMPATback to summary
private static final boolean COMPAT
countback to summary
public int count
decimalAtback to summary
public int decimalAt

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, digits[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'.

digitsback to summary
public byte[] digits
isNegativeback to summary
private boolean isNegative
LONG_MIN_REPback to summary
private static final byte[] LONG_MIN_REP
Annotations
@Stable
MAX_COUNTback to summary
public static final int MAX_COUNT

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.

roundingModeback to summary
private RoundingMode roundingMode

Constructor Detail

DigitListback to summary
pack-priv DigitList()

Method Detail

appendback to summary
public void append(char digit)

Appends a digit to the list, extending the list when necessary.

clearback to summary
public void clear()

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.

cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

Creates a copy of this object.

Returns:Object

a clone of this instance.

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

equality test between two digit lists.

extendDigitsback to summary
private void extendDigits(int len)
fitsIntoLongback to summary
pack-priv boolean fitsIntoLong(boolean isPositive, boolean ignoreNegativeZero)

Return true if the number represented by this object can fit into a long.

Parameters
isPositive:boolean

true if this number should be regarded as positive

ignoreNegativeZero:boolean

true if -0 should be regarded as identical to +0; otherwise they are considered distinct

Returns:boolean

true if this number fits into a Java long

getBigDecimalback to summary
public BigDecimal getBigDecimal()

Utility routine to get the value of the digit list. If (count == 0) this does not throw a NumberFormatException, unlike BigDecimal("").

getDoubleback to summary
public double getDouble()

Utility routine to get the value of the digit list If (count == 0) this returns 0.0, unlike Double.parseDouble("") which throws NumberFormatException.

getLongback to summary
public long getLong()

Utility routine to get the value of the digit list. If (count == 0) this returns 0, unlike Long.parseLong("") which throws NumberFormatException.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Generates the hash code for the digit list.

isZeroback to summary
pack-priv boolean isZero()

Return true if the represented number is zero.

nonZeroAfterIndexback to summary
private boolean nonZeroAfterIndex(int index)

Return true if there exists a non-zero digit in the digit list from the given index until the end.

roundback to summary
private void round(int maximumDigits, boolean alreadyRounded, boolean valueExactAsDecimal)

Round the representation to the given number of digits.

Parameters
maximumDigits:int

The maximum number of digits to be shown.

alreadyRounded:boolean

whether or not rounding up has already happened.

valueExactAsDecimal:boolean

whether or not collected digits provide an exact decimal representation of the value. Upon return, count will be less than or equal to maximumDigits.

roundIntback to summary
private void roundInt(int maximumDigits)

Round the representation to the given number of digits.

Parameters
maximumDigits:int

The maximum number of digits to be shown. Upon return, count will be less than or equal to maximumDigits.

roundUpback to summary
private int roundUp(int maximumDigits)

Round the digit list up numerically. This involves incrementing digits from the LSD to the MSD.

Parameters
maximumDigits:int

The maximum number of digits to be shown.

Returns:int

The new maximum digits after rounding.

setback to summary
pack-priv void set(boolean isNegative, double source, int maximumFractionDigits)

Set the digit list to a representation of the given double value. This method supports fixed-point notation.

Parameters
isNegative:boolean

Boolean value indicating whether the number is negative.

source:double

Value to be converted; must not be Inf, -Inf, Nan, or a value <= 0.

maximumFractionDigits:int

The most fractional digits which should be converted.

setback to summary
pack-priv void set(boolean isNegative, double source, int maximumDigits, boolean fixedPoint)

Set the digit list to a representation of the given double value. This method supports both fixed-point and exponential notation.

Parameters
isNegative:boolean

Boolean value indicating whether the number is negative.

source:double

Value to be converted; must not be Inf, -Inf, Nan, or a value <= 0.

maximumDigits:int

The most fractional or total digits which should be converted.

fixedPoint:boolean

If true, then maximumDigits is the maximum fractional digits to be converted. If false, total digits.

setback to summary
private void set(boolean isNegative, int exp, boolean roundedUp, boolean valueExactAsDecimal, int maximumDigits, boolean fixedPoint)

Generate a representation of the form DDDDD, DDDDD.DDDDD, or DDDDDE+/-DDDDD.

Parameters
roundedUp:boolean

whether or not rounding up has already happened.

valueExactAsDecimal:boolean

whether or not collected digits provide an exact decimal representation of the value.

setback to summary
pack-priv void set(boolean isNegative, long source)

Utility routine to set the value of the digit list from a long

setback to summary
pack-priv void set(boolean isNegative, long source, int maximumDigits)

Set the digit list to a representation of the given long value.

Parameters
isNegative:boolean

Boolean value indicating whether the number is negative.

source:long

Value to be converted; must be >= 0 or == Long.MIN_VALUE.

maximumDigits:int

The most digits which should be converted. If maximumDigits is lower than the number of significant digits in source, the representation will be rounded. Ignored if <= 0.

setback to summary
pack-priv void set(boolean isNegative, BigDecimal source, int maximumDigits, boolean fixedPoint)

Set the digit list to a representation of the given BigDecimal value. This method supports both fixed-point and exponential notation.

Parameters
isNegative:boolean

Boolean value indicating whether the number is negative.

source:BigDecimal

Value to be converted; must not be a value <= 0.

maximumDigits:int

The most fractional or total digits which should be converted.

fixedPoint:boolean

If true, then maximumDigits is the maximum fractional digits to be converted. If false, total digits.

Annotations
@SuppressWarnings:deprecation
setback to summary
pack-priv void set(boolean isNegative, BigInteger source, int maximumDigits)

Set the digit list to a representation of the given BigInteger value.

Parameters
isNegative:boolean

Boolean value indicating whether the number is negative.

source:BigInteger

Value to be converted; must be >= 0.

maximumDigits:int

The most digits which should be converted. If maximumDigits is lower than the number of significant digits in source, the representation will be rounded. Ignored if <= 0.

Annotations
@SuppressWarnings:deprecation
setRoundingModeback to summary
pack-priv void setRoundingMode(RoundingMode r)

Set the rounding mode

shouldRoundUpback to summary
private boolean shouldRoundUp(int maximumDigits, boolean alreadyRounded, boolean valueExactAsDecimal)

Return true if truncating the representation to the given number of digits will result in an increment to the last digit. This method implements the rounding modes defined in the java.math.RoundingMode class. [bnf]

Parameters
maximumDigits:int

the number of digits to keep, from 0 to count-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 "#.#").

alreadyRounded:boolean

whether or not rounding up has already happened.

valueExactAsDecimal:boolean

whether or not collected digits provide an exact decimal representation of the value.

Returns:boolean

true if digit maximumDigits-1 should be incremented

Exceptions
ArithmeticException:
if rounding is needed with rounding mode being set to RoundingMode.UNNECESSARY
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.