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 - 10representing 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 interfaceInterface containing values used during string formatting. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intRadix for decimal numbers.private static final charDecimal separator character.(package private) intNumber 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 intNumber that exponents in engineering format must be a multiple of.(package private) intExponent for the value.private static final charExponent character.private static final charMinus sign character.(package private) final booleanTrue if the value is negative.private char[]Output buffer for use in creating string representations.private intOutput buffer index.private static final intCenter value used when rounding.private static final intNumber of characters in thousands groupings.private static final charZero digit character. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateParsedDecimal(boolean negative, int[] digits, int digitCount, int exponent) Constructs a new instance from its parts. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidappend(char ch) Appends the given character to the output buffer.private voidappend(char[] chars) Appends the given character array directly to the output buffer.private voidappendFraction(int zeroCount, int startIdx, ParsedDecimal.FormatOptions opts) Appends the fractional component of the number to the current output buffer.private voidappendLocalizedDigit(int n, char[] digitChars) Appends the localized representation of the digitnto the output buffer.private intappendWhole(int wholeCount, ParsedDecimal.FormatOptions opts) Appends the whole number portion of this value to the output buffer.private intappendWholeGrouped(int wholeCount, ParsedDecimal.FormatOptions opts) Appends the whole number portion of this value to the output buffer, adding thousands separators as needed.private static intdigitValue(char ch) Gets the numeric value of the given digit character.static ParsedDecimalfrom(double d) Constructs a new instance from the given double value.private intgetDigitStringSize(int decimalPos, ParsedDecimal.FormatOptions opts) Gets the number of characters required for the digit portion of a string representation of this value.intGets the exponent value.private intgetPlainStringSize(int decimalPos, ParsedDecimal.FormatOptions opts) Gets the number of characters required to create a plain format representation of this value.intGet 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) booleanisZero()Returnstrueif this value is equal to zero.voidmaxPrecision(int precision) Ensures that this instance has at most the given number of significant digits (i.e.private StringGets the output buffer as a string.private static intparseExponent(char[] chars, int start) Parses a double exponent value fromchars, starting at thestartindex and continuing through the end of the array.private voidprepareOutput(int size) Prepares the output buffer for a string of the given size.private booleanReturnstrueif a grouping separator should be added after the whole digit character at the given position.voidround(int roundExponent) Rounds the instance to the given decimal exponent position usinghalf-even rounding.private voidroundUp(int count) Rounds the value up to the given number of digits.private voidsetSingleDigitValue(int digit, int newExponent) Sets the value of this instance to a single digit with the given exponent.private booleanshouldIncludeExponent(int targetExponent, ParsedDecimal.FormatOptions opts) Returnstrueif a formatted string with the given target exponent should include the exponent field.private booleanReturnstrueif formatted strings should include the minus sign, considering the value of this instance and the given format options.private booleanshouldRoundUp(int count) Returnstrueif 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 StringtoScientificString(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 voidtruncate(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-trueif the value is negativedigits- array containing significant digitsdigitCount- number of digits used from thedigitsarrayexponent- 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- ifdisNaNor infinite
-
parseExponent
private static int parseExponent(char[] chars, int start) Parses a double exponent value fromchars, starting at thestartindex 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 digitnto the output buffer.- Parameters:
n- digit to appenddigitChars- character array containing localized versions of the digits0-9in 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
digitsappended 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
digitsappended 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 thedigitsarrayopts- 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 thedigitsarrayopts- 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()Returnstrueif this value is equal to zero. The sign field is ignored, meaning that this method will returntruefor both+0and-0.- Returns:
trueif 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) Returnstrueif 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:
trueif 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-2will 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
Returnstrueif a formatted string with the given target exponent should include the exponent field.- Parameters:
targetExponent- exponent of the formatted resultopts- format options- Returns:
trueif the formatted string should include the exponent field
-
shouldIncludeMinus
Returnstrueif formatted strings should include the minus sign, considering the value of this instance and the given format options.- Parameters:
opts- format options- Returns:
trueif a minus sign should be included in the output
-
shouldRoundUp
private boolean shouldRoundUp(int count) Returnstrueif 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:
trueif 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 notationbut 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 thedigitsarray; 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
-