Top Description Inners Fields Constructors Methods
org.apache.commons.text.numbers

pack-priv final Class ParsedDecimal

extends Object
Class Inheritance

Internal class representing a decimal value parsed into separate components. Each number is represented with

Examples

DoubleNegativeDigitsExponent
0.0false[0]0
1.2false[1, 2]-1
-0.00971true[9, 7, 1]-5
56300true[5, 6, 3]2

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface
ParsedDecimal.FormatOptions

Interface containing values used during string formatting.

Field Summary

Modifier and TypeField and Description
private static final int
DECIMAL_RADIX

Radix for decimal numbers.

private static final char
DECIMAL_SEP_CHAR

Decimal separator character.

pack-priv int
digitCount

Number of digits used in the digits array; not necessarily equal to the length.

pack-priv final int[]
digits

Array containing the significant decimal digits for the value.

private static final int
ENG_EXPONENT_MOD

Number that exponents in engineering format must be a multiple of.

pack-priv int
exponent

Exponent for the value.

private static final char
EXPONENT_CHAR

Exponent character.

private static final char
MINUS_CHAR

Minus sign character.

pack-priv final boolean
negative

True if the value is negative.

private char[]
outputChars

Output buffer for use in creating string representations.

private int
outputIdx

Output buffer index.

private static final int
ROUND_CENTER

Center value used when rounding.

private static final int
THOUSANDS_GROUP_SIZE

Number of characters in thousands groupings.

private static final char
ZERO_CHAR

Zero digit character.

Constructor Summary

AccessConstructor and Description
private
ParsedDecimal(final boolean
true if the value is negative.
negative
,
final int[]
array containing significant digits.
digits
,
final int
number of digits used from the digits array.
digitCount
,
final int
exponent value.
exponent
)

Constructs a new instance from its parts.

Method Summary

Modifier and TypeMethod and Description
private void
append(final char
character to append.
ch
)

Appends the given character to the output buffer.

private void
append(final char[]
characters to append.
chars
)

Appends the given character array directly to the output buffer.

private void
appendFraction(final int
number of zeros to add after the decimal point and before the first significant digit.
zeroCount
,
final int
significant digit start index.
startIdx
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Appends the fractional component of the number to the current output buffer.

private void
appendLocalizedDigit(final int
digit to append.
n
,
final char[]
character array containing localized versions of the digits 0-9 in that order.
digitChars
)

Appends the localized representation of the digit n to the output buffer.

private int

Returns:

number of digits from digits appended to the output buffer.
appendWhole
(final int
total number of digits required to the left of the decimal point.
wholeCount
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Appends the whole number portion of this value to the output buffer.

private int

Returns:

number of digits from digits appended to the output buffer.
appendWholeGrouped
(final int
total number of digits required to the right of the decimal point
wholeCount
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Appends the whole number portion of this value to the output buffer, adding thousands separators as needed.

private static int

Returns:

numeric value of the digit character, ex: '1' = 1.
digitValue
(final char
digit character.
ch
)

Gets the numeric value of the given digit character.

public static ParsedDecimal

Returns:

a new instance containing the parsed components of the given double value.
from
(final double
double value.
d
)

Constructs a new instance from the given double value.

private int

Returns:

number of characters required for the digit portion of a string representation of this value.
getDigitStringSize
(final int
decimal point position relative to the digits array.
decimalPos
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Gets the number of characters required for the digit portion of a string representation of this value.

public int

Returns:

exponent value.
getExponent
()

Gets the exponent value.

private int

Returns:

number of characters in the plain string representation of this value, created using the given parameters.
getPlainStringSize
(final int
decimal position relative to the digits array.
decimalPos
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Gets the number of characters required to create a plain format representation of this value.

public int

Returns:

the exponent that would be used when representing this number in scientific notation.
getScientificExponent
()

Gets the exponent that would be used when representing this number in scientific notation (i.e., with a single non-zero digit in front of the decimal point).

pack-priv boolean

Returns:

true if the value is equal to zero.
isZero
()

Tests true if this value is equal to zero.

public void
maxPrecision(final int
maximum number of significant digits to include.
precision
)

Ensures that this instance has at most the given number of significant digits (i.e. precision).

private String

Returns:

output buffer as a string
outputString
()

Gets the output buffer as a string.

private static int

Returns:

parsed exponent value.
parseExponent
(final char[]
character array to parse a double exponent value from.
chars
,
final int
start index.
start
)

Parses a double exponent value from chars, starting at the start index and continuing through the end of the array.

private void
prepareOutput(final int
buffer size.
size
)

Prepares the output buffer for a string of the given size.

private boolean

Returns:

true if a grouping separator should be added.
requiresGroupingSeparatorAfterPosition
(final int
whole digit character position, with values starting at 1 and increasing from right to left.
pos
)

Returns true if a grouping separator should be added after the whole digit character at the given position.

public void
round(final int
exponent defining the decimal place to round to.
roundExponent
)

Rounds the instance to the given decimal exponent position using half-even rounding.

private void
roundUp(final int
target number of digits; must be greater than zero and less than the current number of digits.
count
)

Rounds the value up to the given number of digits.

private void
setSingleDigitValue(final int
digit value.
digit
,
final int
new exponent value.
newExponent
)

Sets the value of this instance to a single digit with the given exponent.

private boolean

Returns:

true if the formatted string should include the exponent field.
shouldIncludeExponent
(final int
exponent of the formatted result.
targetExponent
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Returns true if a formatted string with the given target exponent should include the exponent field.

private boolean

Returns:

true if a minus sign should be included in the output.
shouldIncludeMinus
(final ParsedDecimal.FormatOptions
format options.
opts
)

Returns true if formatted strings should include the minus sign, considering the value of this instance and the given format options.

private boolean

Returns:

true if a rounding operation for the given number of digits should round up.
shouldRoundUp
(final int
number of digits to round to; must be greater than zero and less than the current number of digits.
count
)

Returns true if a rounding operation for the given number of digits should round up.

public String

Returns:

value in engineering format.
toEngineeringString
(final ParsedDecimal.FormatOptions
format options.
opts
)

Returns a string representation of this value in engineering notation.

public String

Returns:

value in plain format.
toPlainString
(final ParsedDecimal.FormatOptions
format options.
opts
)

Returns a string representation of this value with no exponent field.

public String

Returns:

value in scientific format.
toScientificString
(final ParsedDecimal.FormatOptions
format options.
opts
)

Returns a string representation of this value in scientific notation.

private String

Returns:

value in scientific format.
toScientificString
(final int
decimal position relative to the digits array; this value is expected to be greater than 0.
decimalPos
,
final ParsedDecimal.FormatOptions
format options.
opts
)

Returns a string representation of the value in scientific notation using the given decimal point position.

private void
truncate(final int
number of digits; must be greater than zero and less than the current number of digits.
count
)

Truncates the value to the given number of digits.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DECIMAL_RADIXback to summary
private static final int DECIMAL_RADIX

Radix for decimal numbers.

DECIMAL_SEP_CHARback to summary
private static final char DECIMAL_SEP_CHAR

Decimal separator character.

digitCountback to summary
pack-priv int digitCount

Number of digits used in the digits array; not necessarily equal to the length.

digitsback to summary
pack-priv final int[] digits

Array containing the significant decimal digits for the value.

ENG_EXPONENT_MODback to summary
private static final int ENG_EXPONENT_MOD

Number that exponents in engineering format must be a multiple of.

exponentback to summary
pack-priv int exponent

Exponent for the value.

EXPONENT_CHARback to summary
private static final char EXPONENT_CHAR

Exponent character.

MINUS_CHARback to summary
private static final char MINUS_CHAR

Minus sign character.

negativeback to summary
pack-priv final boolean negative

True if the value is negative.

outputCharsback to summary
private char[] outputChars

Output buffer for use in creating string representations.

outputIdxback to summary
private int outputIdx

Output buffer index.

ROUND_CENTERback to summary
private static final int ROUND_CENTER

Center value used when rounding.

THOUSANDS_GROUP_SIZEback to summary
private static final int THOUSANDS_GROUP_SIZE

Number of characters in thousands groupings.

ZERO_CHARback to summary
private static final char ZERO_CHAR

Zero digit character.

Constructor Detail

ParsedDecimalback to summary
private ParsedDecimal(final boolean negative, final int[] digits, final int digitCount, final int exponent)

Constructs a new instance from its parts.

Parameters
negative:boolean

true if the value is negative.

digits:int[]

array containing significant digits.

digitCount:int

number of digits used from the digits array.

exponent:int

exponent value.

Method Detail

appendback to summary
private void append(final char ch)

Appends the given character to the output buffer.

Parameters
ch:char

character to append.

appendback to summary
private void append(final char[] chars)

Appends the given character array directly to the output buffer.

Parameters
chars:char[]

characters to append.

appendFractionback to summary
private void appendFraction(final int zeroCount, final int startIdx, final ParsedDecimal.FormatOptions opts)

Appends the fractional component of the number to the current output buffer.

Parameters
zeroCount:int

number of zeros to add after the decimal point and before the first significant digit.

startIdx:int

significant digit start index.

opts:ParsedDecimal.FormatOptions

format options.

appendLocalizedDigitback to summary
private void appendLocalizedDigit(final int n, final char[] digitChars)

Appends the localized representation of the digit n to the output buffer.

Parameters
n:int

digit to append.

digitChars:char[]

character array containing localized versions of the digits 0-9 in that order.

appendWholeback to summary
private int appendWhole(final int wholeCount, final ParsedDecimal.FormatOptions opts)

Appends the whole number portion of this value to the output buffer. No thousands separators are added.

Parameters
wholeCount:int

total number of digits required to the left of the decimal point.

opts:ParsedDecimal.FormatOptions

format options.

Returns:int

number of digits from digits appended to the output buffer.

See Also
appendWholeGrouped(int, FormatOptions)
appendWholeGroupedback to summary
private int appendWholeGrouped(final int wholeCount, final ParsedDecimal.FormatOptions opts)

Appends the whole number portion of this value to the output buffer, adding thousands separators as needed.

Parameters
wholeCount:int

total number of digits required to the right of the decimal point

opts:ParsedDecimal.FormatOptions

format options.

Returns:int

number of digits from digits appended to the output buffer.

See Also
appendWhole(int, FormatOptions)
digitValueback to summary
private static int digitValue(final char ch)

Gets the numeric value of the given digit character. No validation of the character type is performed.

Parameters
ch:char

digit character.

Returns:int

numeric value of the digit character, ex: '1' = 1.

fromback to summary
public static ParsedDecimal from(final double d)

Constructs a new instance from the given double value.

Parameters
d:double

double value.

Returns:ParsedDecimal

a new instance containing the parsed components of the given double value.

Exceptions
IllegalArgumentException:
if d is NaN or infinite.
getDigitStringSizeback to summary
private int getDigitStringSize(final int decimalPos, final ParsedDecimal.FormatOptions opts)

Gets the number of characters required for the digit portion of a string representation of this value. This excludes any exponent or thousands groupings characters.

Parameters
decimalPos:int

decimal point position relative to the digits array.

opts:ParsedDecimal.FormatOptions

format options.

Returns:int

number of characters required for the digit portion of a string representation of this value.

getExponentback to summary
public int getExponent()

Gets the exponent value. This exponent produces a floating point value with the correct magnitude when applied to the internal unsigned integer.

Returns:int

exponent value.

getPlainStringSizeback to summary
private int getPlainStringSize(final int decimalPos, final ParsedDecimal.FormatOptions opts)

Gets the number of characters required to create a plain format representation of this value.

Parameters
decimalPos:int

decimal position relative to the digits array.

opts:ParsedDecimal.FormatOptions

format options.

Returns:int

number of characters in the plain string representation of this value, created using the given parameters.

getScientificExponentback to summary
public int getScientificExponent()

Gets the exponent that would be used when representing this number in scientific notation (i.e., with a single non-zero digit in front of the decimal point).

Returns:int

the exponent that would be used when representing this number in scientific notation.

isZeroback to summary
pack-priv boolean isZero()

Tests true if this value is equal to zero. The sign field is ignored, meaning that this method will return true for both +0 and -0.

Returns:boolean

true if the value is equal to zero.

maxPrecisionback to summary
public void maxPrecision(final int precision)

Ensures that this instance has at most the given number of significant digits (i.e. precision). If this instance already has a precision less than or equal to the argument, nothing is done. If the given precision requires a reduction in the number of digits, then the value is rounded using half-even rounding.

Parameters
precision:int

maximum number of significant digits to include.

outputStringback to summary
private String outputString()

Gets the output buffer as a string.

Returns:String

output buffer as a string

parseExponentback to summary
private static int parseExponent(final char[] chars, final int start)

Parses a double exponent value from chars, starting at the start index and continuing through the end of the array.

Parameters
chars:char[]

character array to parse a double exponent value from.

start:int

start index.

Returns:int

parsed exponent value.

prepareOutputback to summary
private void prepareOutput(final int size)

Prepares the output buffer for a string of the given size.

Parameters
size:int

buffer size.

requiresGroupingSeparatorAfterPositionback to summary
private boolean requiresGroupingSeparatorAfterPosition(final int pos)

Returns true if a grouping separator should be added after the whole digit character at the given position.

Parameters
pos:int

whole digit character position, with values starting at 1 and increasing from right to left.

Returns:boolean

true if a grouping separator should be added.

roundback to summary
public void round(final int roundExponent)

Rounds the instance to the given decimal exponent position using half-even rounding. For example, a value of -2 will round the instance to the digit at the position 10-2 (i.e. to the closest multiple of 0.01).

Parameters
roundExponent:int

exponent defining the decimal place to round to.

roundUpback to summary
private void roundUp(final int count)

Rounds the value up to the given number of digits.

Parameters
count:int

target number of digits; must be greater than zero and less than the current number of digits.

setSingleDigitValueback to summary
private void setSingleDigitValue(final int digit, final int newExponent)

Sets the value of this instance to a single digit with the given exponent. The sign of the value is retained.

Parameters
digit:int

digit value.

newExponent:int

new exponent value.

shouldIncludeExponentback to summary
private boolean shouldIncludeExponent(final int targetExponent, final ParsedDecimal.FormatOptions opts)

Returns true if a formatted string with the given target exponent should include the exponent field.

Parameters
targetExponent:int

exponent of the formatted result.

opts:ParsedDecimal.FormatOptions

format options.

Returns:boolean

true if the formatted string should include the exponent field.

shouldIncludeMinusback to summary
private boolean shouldIncludeMinus(final ParsedDecimal.FormatOptions opts)

Returns true if formatted strings should include the minus sign, considering the value of this instance and the given format options.

Parameters
opts:ParsedDecimal.FormatOptions

format options.

Returns:boolean

true if a minus sign should be included in the output.

shouldRoundUpback to summary
private boolean shouldRoundUp(final int count)

Returns true if a rounding operation for the given number of digits should round up.

Parameters
count:int

number of digits to round to; must be greater than zero and less than the current number of digits.

Returns:boolean

true if a rounding operation for the given number of digits should round up.

toEngineeringStringback to summary
public String toEngineeringString(final ParsedDecimal.FormatOptions opts)

Returns a string representation of this value in engineering notation. This is similar to scientific notation but with the exponent forced to be a multiple of 3, allowing easier alignment with SI prefixes.

For example:

0 = "0.0"
10 = "10.0"
1e-6 = "1.0E-6"
1e11 = "100.0E9"
Parameters
opts:ParsedDecimal.FormatOptions

format options.

Returns:String

value in engineering format.

toPlainStringback to summary
public String toPlainString(final ParsedDecimal.FormatOptions opts)

Returns a string representation of this value with no exponent field.

For example:

10 = "10.0"
1e-6 = "0.000001"
1e11 = "100000000000.0"
Parameters
opts:ParsedDecimal.FormatOptions

format options.

Returns:String

value in plain format.

toScientificStringback to summary
public String toScientificString(final ParsedDecimal.FormatOptions opts)

Returns a string representation of this value in scientific notation.

For example:

0 = "0.0"
10 = "1.0E1"
1e-6 = "1.0E-6"
1e11 = "1.0E11"
Parameters
opts:ParsedDecimal.FormatOptions

format options.

Returns:String

value in scientific format.

toScientificStringback to summary
private String toScientificString(final int decimalPos, final ParsedDecimal.FormatOptions opts)

Returns a string representation of the value in scientific notation using the given decimal point position.

Parameters
decimalPos:int

decimal position relative to the digits array; this value is expected to be greater than 0.

opts:ParsedDecimal.FormatOptions

format options.

Returns:String

value in scientific format.

truncateback to summary
private void truncate(final int count)

Truncates the value to the given number of digits.

Parameters
count:int

number of digits; must be greater than zero and less than the current number of digits.

org.apache.commons.text.numbers back to summary

pack-priv Interface ParsedDecimal.FormatOptions

Known Direct Implementers
org.apache.commons.text.numbers.DoubleFormat.AbstractDoubleFormat

Interface containing values used during string formatting.

Method Summary

Modifier and TypeMethod and Description
public char

Returns:

decimal separator character
getDecimalSeparator
()

Gets the decimal separator character.

public char[]

Returns:

array containing the digit characters 0-9.
getDigits
()

Gets an array containing the localized digit characters 0-9 in that order.

public char[]

Returns:

exponent separator as an array of characters.
getExponentSeparatorChars
()

Gets the exponent separator as an array of characters.

public char

Returns:

character used to separate thousands groupings.
getGroupingSeparator
()

Gets the character used to separate thousands groupings.

public char

Returns:

minus sign character.
getMinusSign
()

Gets the minus sign character.

public boolean

Returns:

true if exponent values should always be included.
isAlwaysIncludeExponent
()

Return true if exponent values should always be included in formatted output, even if the value is zero.

public boolean

Returns:

true if thousand should be grouped.
isGroupThousands
()

Return true if thousands should be grouped.

public boolean

Returns:

true if fraction placeholders should be included.
isIncludeFractionPlaceholder
()

Return true if fraction placeholders (e.g., ".0" in "1.0") should be included.

public boolean

Returns:

true if the minus zero string should be allowed.
isSignedZero
()

Return true if the string zero should be prefixed with the minus sign for negative zero values.

Method Detail

getDecimalSeparatorback to summary
public char getDecimalSeparator()

Gets the decimal separator character.

Returns:char

decimal separator character

getDigitsback to summary
public char[] getDigits()

Gets an array containing the localized digit characters 0-9 in that order. This string must be non-null and have a length of 10.

Returns:char[]

array containing the digit characters 0-9.

getExponentSeparatorCharsback to summary
public char[] getExponentSeparatorChars()

Gets the exponent separator as an array of characters.

Returns:char[]

exponent separator as an array of characters.

getGroupingSeparatorback to summary
public char getGroupingSeparator()

Gets the character used to separate thousands groupings.

Returns:char

character used to separate thousands groupings.

getMinusSignback to summary
public char getMinusSign()

Gets the minus sign character.

Returns:char

minus sign character.

isAlwaysIncludeExponentback to summary
public boolean isAlwaysIncludeExponent()

Return true if exponent values should always be included in formatted output, even if the value is zero.

Returns:boolean

true if exponent values should always be included.

isGroupThousandsback to summary
public boolean isGroupThousands()

Return true if thousands should be grouped.

Returns:boolean

true if thousand should be grouped.

isIncludeFractionPlaceholderback to summary
public boolean isIncludeFractionPlaceholder()

Return true if fraction placeholders (e.g., ".0" in "1.0") should be included.

Returns:boolean

true if fraction placeholders should be included.

isSignedZeroback to summary
public boolean isSignedZero()

Return true if the string zero should be prefixed with the minus sign for negative zero values.

Returns:boolean

true if the minus zero string should be allowed.