Package org.apache.commons.text.numbers
Class ParsedDecimal
java.lang.Object
org.apache.commons.text.numbers.ParsedDecimal
Internal class representing a decimal value parsed into separate components. Each number
is represented with
- a boolean flag for the sign,
- a sequence of the digits
0 - 10
representing an unsigned integer with leading and trailing zeros removed, and - an exponent value that when applied to the base 10 digits produces a floating point value with the correct magnitude.
Examples
Double | Negative | Digits | Exponent |
---|---|---|---|
0.0 | false | [0] | 0 |
1.2 | false | [1, 2] | -1 |
-0.00971 | true | [9, 7, 1] | -5 |
56300 | true | [5, 6, 3] | 2 |
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static interface
Interface containing values used during string formatting. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Radix for decimal numbers.private static final char
Decimal separator character.(package private) int
Number of digits used in the digits array; not necessarily equal to the length.(package private) final int[]
Array containing the significant decimal digits for the value.private static final int
Number that exponents in engineering format must be a multiple of.(package private) int
Exponent for the value.private static final char
Exponent character.private static final char
Minus sign character.(package private) final boolean
True if the value is negative.private char[]
Output buffer for use in creating string representations.private int
Output buffer index.private static final int
Center value used when rounding.private static final int
Number of characters in thousands groupings.private static final char
Zero digit character. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
ParsedDecimal
(boolean negative, int[] digits, int digitCount, int exponent) Constructs a new instance from its parts. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
append
(char ch) Appends the given character to the output buffer.private void
append
(char[] chars) Appends the given character array directly to the output buffer.private void
appendFraction
(int zeroCount, int startIdx, ParsedDecimal.FormatOptions opts) Appends the fractional component of the number to the current output buffer.private void
appendLocalizedDigit
(int n, char[] digitChars) Appends the localized representation of the digitn
to the output buffer.private int
appendWhole
(int wholeCount, ParsedDecimal.FormatOptions opts) Appends the whole number portion of this value to the output buffer.private int
appendWholeGrouped
(int wholeCount, ParsedDecimal.FormatOptions opts) Appends the whole number portion of this value to the output buffer, adding thousands separators as needed.private static int
digitValue
(char ch) Gets the numeric value of the given digit character.static ParsedDecimal
from
(double d) Constructs a new instance from the given double value.private int
getDigitStringSize
(int decimalPos, ParsedDecimal.FormatOptions opts) Gets the number of characters required for the digit portion of a string representation of this value.int
Gets the exponent value.private int
getPlainStringSize
(int decimalPos, ParsedDecimal.FormatOptions opts) Gets the number of characters required to create a plain format representation of this value.int
Get sthe 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).(package private) boolean
isZero()
Returnstrue
if this value is equal to zero.void
maxPrecision
(int precision) Ensures that this instance has at most the given number of significant digits (i.e.private String
Gets the output buffer as a string.private static int
parseExponent
(char[] chars, int start) Parses a double exponent value fromchars
, starting at thestart
index and continuing through the end of the array.private void
prepareOutput
(int size) Prepares the output buffer for a string of the given size.private boolean
Returnstrue
if a grouping separator should be added after the whole digit character at the given position.void
round
(int roundExponent) Rounds the instance to the given decimal exponent position usinghalf-even rounding
.private void
roundUp
(int count) Rounds the value up to the given number of digits.private void
setSingleDigitValue
(int digit, int newExponent) Sets the value of this instance to a single digit with the given exponent.private boolean
shouldIncludeExponent
(int targetExponent, ParsedDecimal.FormatOptions opts) Returnstrue
if a formatted string with the given target exponent should include the exponent field.private boolean
Returnstrue
if formatted strings should include the minus sign, considering the value of this instance and the given format options.private boolean
shouldRoundUp
(int count) Returnstrue
if a rounding operation for the given number of digits should round up.Returns a string representation of this value in engineering notation.Returns a string representation of this value with no exponent field.private String
toScientificString
(int decimalPos, ParsedDecimal.FormatOptions opts) Returns a string representation of the value in scientific notation using the given decimal point position.Returns a string representation of this value in scientific notation.private void
truncate
(int count) Truncates the value to the given number of digits.
-
Field Details
-
MINUS_CHAR
private static final char MINUS_CHARMinus sign character.- See Also:
-
DECIMAL_SEP_CHAR
private static final char DECIMAL_SEP_CHARDecimal separator character.- See Also:
-
EXPONENT_CHAR
private static final char EXPONENT_CHARExponent character.- See Also:
-
ZERO_CHAR
private static final char ZERO_CHARZero digit character.- See Also:
-
THOUSANDS_GROUP_SIZE
private static final int THOUSANDS_GROUP_SIZENumber of characters in thousands groupings.- See Also:
-
DECIMAL_RADIX
private static final int DECIMAL_RADIXRadix for decimal numbers.- See Also:
-
ROUND_CENTER
private static final int ROUND_CENTERCenter value used when rounding.- See Also:
-
ENG_EXPONENT_MOD
private static final int ENG_EXPONENT_MODNumber that exponents in engineering format must be a multiple of.- See Also:
-
negative
final boolean negativeTrue if the value is negative. -
digits
final int[] digitsArray containing the significant decimal digits for the value. -
digitCount
int digitCountNumber of digits used in the digits array; not necessarily equal to the length. -
exponent
int exponentExponent for the value. -
outputChars
private char[] outputCharsOutput buffer for use in creating string representations. -
outputIdx
private int outputIdxOutput buffer index.
-
-
Constructor Details
-
ParsedDecimal
private ParsedDecimal(boolean negative, int[] digits, int digitCount, int exponent) Constructs a new instance from its parts.- Parameters:
negative
-true
if the value is negativedigits
- array containing significant digitsdigitCount
- number of digits used from thedigits
arrayexponent
- exponent value
-
-
Method Details
-
digitValue
private static int digitValue(char ch) Gets the numeric value of the given digit character. No validation of the character type is performed.- Parameters:
ch
- digit character- Returns:
- numeric value of the digit character, ex: '1' = 1
-
from
Constructs a new instance from the given double value.- Parameters:
d
- double value- Returns:
- a new instance containing the parsed components of the given double value
- Throws:
IllegalArgumentException
- ifd
isNaN
or infinite
-
parseExponent
private static int parseExponent(char[] chars, int start) Parses a double exponent value fromchars
, starting at thestart
index and continuing through the end of the array.- Parameters:
chars
- character array to parse a double exponent value fromstart
- start index- Returns:
- parsed exponent value
-
append
private void append(char ch) Appends the given character to the output buffer.- Parameters:
ch
- character to append
-
append
private void append(char[] chars) Appends the given character array directly to the output buffer.- Parameters:
chars
- characters to append
-
appendFraction
Appends the fractional component of the number to the current output buffer.- Parameters:
zeroCount
- number of zeros to add after the decimal point and before the first significant digitstartIdx
- significant digit start indexopts
- format options
-
appendLocalizedDigit
private void appendLocalizedDigit(int n, char[] digitChars) Appends the localized representation of the digitn
to the output buffer.- Parameters:
n
- digit to appenddigitChars
- character array containing localized versions of the digits0-9
in that order
-
appendWhole
Appends the whole number portion of this value to the output buffer. No thousands separators are added.- Parameters:
wholeCount
- total number of digits required to the left of the decimal pointopts
- format options- Returns:
- number of digits from
digits
appended to the output buffer - See Also:
-
appendWholeGrouped
Appends the whole number portion of this value to the output buffer, adding thousands separators as needed.- Parameters:
wholeCount
- total number of digits required to the right of the decimal pointopts
- format options- Returns:
- number of digits from
digits
appended to the output buffer - See Also:
-
getDigitStringSize
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
- decimal point position relative to thedigits
arrayopts
- format options- Returns:
- number of characters required for the digit portion of a string representation of this value
-
getExponent
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:
- exponent value
-
getPlainStringSize
Gets the number of characters required to create a plain format representation of this value.- Parameters:
decimalPos
- decimal position relative to thedigits
arrayopts
- format options- Returns:
- number of characters in the plain string representation of this value, created using the given parameters
-
getScientificExponent
public int getScientificExponent()Get sthe 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:
- the exponent that would be used when representing this number in scientific notation
-
isZero
boolean isZero()Returnstrue
if this value is equal to zero. The sign field is ignored, meaning that this method will returntrue
for both+0
and-0
.- Returns:
true
if the value is equal to zero
-
maxPrecision
public void maxPrecision(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 usinghalf-even rounding
.- Parameters:
precision
- maximum number of significant digits to include
-
outputString
Gets the output buffer as a string.- Returns:
- output buffer as a string
-
prepareOutput
private void prepareOutput(int size) Prepares the output buffer for a string of the given size.- Parameters:
size
- buffer size
-
requiresGroupingSeparatorAfterPosition
private boolean requiresGroupingSeparatorAfterPosition(int pos) Returnstrue
if a grouping separator should be added after the whole digit character at the given position.- Parameters:
pos
- whole digit character position, with values starting at 1 and increasing from right to left.- Returns:
true
if a grouping separator should be added
-
round
public void round(int roundExponent) Rounds the instance to the given decimal exponent position usinghalf-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
- exponent defining the decimal place to round to
-
roundUp
private void roundUp(int count) Rounds the value up to the given number of digits.- Parameters:
count
- target number of digits; must be greater than zero and less than the current number of digits
-
setSingleDigitValue
private void setSingleDigitValue(int digit, 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
- digit valuenewExponent
- new exponent value
-
shouldIncludeExponent
Returnstrue
if a formatted string with the given target exponent should include the exponent field.- Parameters:
targetExponent
- exponent of the formatted resultopts
- format options- Returns:
true
if the formatted string should include the exponent field
-
shouldIncludeMinus
Returnstrue
if formatted strings should include the minus sign, considering the value of this instance and the given format options.- Parameters:
opts
- format options- Returns:
true
if a minus sign should be included in the output
-
shouldRoundUp
private boolean shouldRoundUp(int count) Returnstrue
if a rounding operation for the given number of digits should round up.- Parameters:
count
- number of digits to round to; must be greater than zero and less than the current number of digits- Returns:
true
if a rounding operation for the given number of digits should round up
-
toEngineeringString
Returns a string representation of this value in engineering notation. This is similar toscientific notation
but with the exponent forced to be a multiple of 3, allowing easier alignment with SI prefixes.0 = "0.0" 10 = "10.0" 1e-6 = "1.0E-6" 1e11 = "100.0E9"
- Parameters:
opts
- format options- Returns:
- value in engineering format
-
toPlainString
Returns a string representation of this value with no exponent field. Ex:10 = "10.0" 1e-6 = "0.000001" 1e11 = "100000000000.0"
- Parameters:
opts
- format options- Returns:
- value in plain format
-
toScientificString
Returns a string representation of this value in scientific notation. Ex:0 = "0.0" 10 = "1.0E1" 1e-6 = "1.0E-6" 1e11 = "1.0E11"
- Parameters:
opts
- format options- Returns:
- value in scientific format
-
toScientificString
Returns a string representation of the value in scientific notation using the given decimal point position.- Parameters:
decimalPos
- decimal position relative to thedigits
array; this value is expected to be greater than 0opts
- format options- Returns:
- value in scientific format
-
truncate
private void truncate(int count) Truncates the value to the given number of digits.- Parameters:
count
- number of digits; must be greater than zero and less than the current number of digits
-