Class DecimalQuantity_AbstractBCD
- All Implemented Interfaces:
DecimalQuantity,PluralRules.IFixedDecimal
- Direct Known Subclasses:
DecimalQuantity_DualStorageBCD
-
Field Summary
FieldsModifier and TypeFieldDescriptionbooleanDeprecated.This API is ICU internal only.protected intThe value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific).protected byteA bitmask of properties relating to the number represented by this object.protected static final intprotected booleanWhether the value in the BCD comes from the double fast path without having been rounded to ensure correctnessprotected intprotected static final intprotected static final intprotected intThe change in magnitude relative to the original double.protected doubleThe original number provided by the user and which is represented in BCD.protected intThe number of digits in the BCD.protected intprotected intThe power of ten corresponding to the least significant digit in the BCD. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadjustExponent(int delta) Adjusts the value for the (suppressed) exponent stored when using notation with exponents (ex: compact, scientific).voidadjustMagnitude(int delta) Scales the number by a power of ten.voidappendDigit(byte value, int leadingZeros, boolean appendAsInteger) Deprecated.This API is ICU internal only.voidapplyMaxInteger(int maxInt) Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.protected abstract BigDecimalReturns a BigDecimal encoding the internal BCD value.clear()protected abstract voidcompact()Removes trailing zeros from the BCD (adjusting the scale as required) and then computes the precision.protected abstract voidcopyBcdFrom(DecimalQuantity _other) voidcopyFrom(DecimalQuantity _other) Sets this instance to be equal to another instance.booleanbooleanReturns whether or not a Long can fully represent the value stored in this DecimalQuantity.bytegetDigit(int magnitude) Gets the digit at the specified magnitude.protected abstract bytegetDigitPos(int position) Returns a single digit from the BCD list.intintGets the smallest power of ten that needs to be displayed.intdoublegetPluralOperand(PluralRules.Operand operand) Returns the value corresponding to the specified operand (n, i, f, t, v, or w).longThis method is for internal testing only.getStandardPlural(PluralRules rules) Computes the plural form for this number based on the specified set of rules.intGets the largest power of ten that needs to be displayed.booleanWhether the number has no nonzero fraction digits.booleanbooleanisNaN()booleanbooleanvoidmultiplyBy(BigDecimal multiplicand) Multiply the internal value.voidnegate()Flips the sign from positive to negative and back.protected abstract voidpopFromLeft(int numDigits) Directly removes digits from the front of the BCD list.voidIf the givenFieldPositionis aUFieldPosition, populates it with the fraction length and fraction long value.protected abstract voidreadBigIntegerToBcd(BigInteger input) Sets the internal BCD state to represent the value in the given BigInteger.protected abstract voidreadIntToBcd(int input) Sets the internal BCD state to represent the value in the given int.protected abstract voidreadLongToBcd(long input) Sets the internal BCD state to represent the value in the given long.voidResets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.voidroundToIncrement(BigDecimal roundingIncrement, MathContext mathContext) Rounds the number to a specified interval, such as 0.05.voidRounds the number to an infinite number of decimal points.voidroundToMagnitude(int magnitude, MathContext mathContext) Rounds the number to a specified magnitude (power of ten).voidroundToNickel(int magnitude, MathContext mathContext) Rounds the number to the nearest multiple of 5 at the specified magnitude.protected abstract voidSets the internal representation to zero.protected abstract voidsetDigitPos(int position, byte value) Sets the digit in the BCD list.voidsetMinFraction(int minFrac) Sets the minimum fraction digits that thisDecimalQuantityshould generate.voidsetMinInteger(int minInt) Sets the minimum integer digits that thisDecimalQuantityshould generate.voidSets the internal BCD state to represent the value in the given BigDecimal.voidvoidsetToDouble(double n) Sets the internal BCD state to represent the value in the given double.voidsetToInt(int n) voidsetToLong(long n) protected abstract voidshiftLeft(int numDigits) Adds zeros to the end of the BCD list.protected abstract voidshiftRight(int numDigits) Removes digits from the end of the BCD list.signum()doubletoDouble()Returns a double approximating the internal BCD.Returns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples.longtoFractionLong(boolean includeTrailingZeros) This returns a long representing the fraction digits of the number, as required by PluralRules.longtoLong(boolean truncateIfOverflow) Returns a long approximating the decimal quantity.Returns the string in "plain" format (no exponential notation) using ASCII digits.voidtoPlainString(StringBuilder result) voidtoScientificString(StringBuilder result) voidtruncate()Removes all fraction digits.Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.graalvm.shadowed.com.ibm.icu.impl.number.DecimalQuantity
createCopy, maxRepresentableDigits
-
Field Details
-
scale
protected int scaleThe power of ten corresponding to the least significant digit in the BCD. For example, if this object represents the number "3.14", the BCD will be "0x314" and the scale will be -2.Note that in
BigDecimal, the scale is defined differently: the number of digits after the decimal place, which is the negative of our definition of scale. -
precision
protected int precisionThe number of digits in the BCD. For example, "1007" has BCD "0x1007" and precision 4. A long cannot represent precisions greater than 16.This value must be re-calculated whenever the value in bcd changes by using
.invalid reference
#computePrecisionAndCompact() -
flags
protected byte flagsA bitmask of properties relating to the number represented by this object.- See Also:
-
NEGATIVE_FLAG
protected static final int NEGATIVE_FLAG- See Also:
-
INFINITY_FLAG
protected static final int INFINITY_FLAG- See Also:
-
NAN_FLAG
protected static final int NAN_FLAG- See Also:
-
origDouble
protected double origDoubleThe original number provided by the user and which is represented in BCD. Used when we need to re-compute the BCD for an exact double representation. -
origDelta
protected int origDeltaThe change in magnitude relative to the original double. Used when we need to re-compute the BCD for an exact double representation. -
isApproximate
protected boolean isApproximateWhether the value in the BCD comes from the double fast path without having been rounded to ensure correctness -
lReqPos
protected int lReqPos -
rReqPos
protected int rReqPos -
exponent
protected int exponentThe value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific). -
explicitExactDouble
Deprecated.This API is ICU internal only.Whether thisDecimalQuantity_DualStorageBCDhas been explicitly converted to an exact double. true if backed by a double that was explicitly converted via convertToAccurateDouble; false otherwise. Used for testing.
-
-
Constructor Details
-
DecimalQuantity_AbstractBCD
public DecimalQuantity_AbstractBCD()
-
-
Method Details
-
copyFrom
Description copied from interface:DecimalQuantitySets this instance to be equal to another instance.- Specified by:
copyFromin interfaceDecimalQuantity- Parameters:
_other- The instance to copy from.
-
clear
-
setMinInteger
public void setMinInteger(int minInt) Description copied from interface:DecimalQuantitySets the minimum integer digits that thisDecimalQuantityshould generate. This method does not perform rounding.- Specified by:
setMinIntegerin interfaceDecimalQuantity- Parameters:
minInt- The minimum number of integer digits.
-
setMinFraction
public void setMinFraction(int minFrac) Description copied from interface:DecimalQuantitySets the minimum fraction digits that thisDecimalQuantityshould generate. This method does not perform rounding.- Specified by:
setMinFractionin interfaceDecimalQuantity- Parameters:
minFrac- The minimum number of fraction digits.
-
applyMaxInteger
public void applyMaxInteger(int maxInt) Description copied from interface:DecimalQuantityTruncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.- Specified by:
applyMaxIntegerin interfaceDecimalQuantity- Parameters:
maxInt- The maximum number of integer digits.
-
getPositionFingerprint
public long getPositionFingerprint()Description copied from interface:DecimalQuantityThis method is for internal testing only.- Specified by:
getPositionFingerprintin interfaceDecimalQuantity
-
roundToIncrement
Description copied from interface:DecimalQuantityRounds the number to a specified interval, such as 0.05.If rounding to a power of ten, use the more efficient
DecimalQuantity.roundToMagnitude(int, java.math.MathContext)instead.- Specified by:
roundToIncrementin interfaceDecimalQuantity- Parameters:
roundingIncrement- The increment to which to round.mathContext- TheMathContextto use if rounding is necessary. Undefined behavior if null.
-
multiplyBy
Description copied from interface:DecimalQuantityMultiply the internal value.- Specified by:
multiplyByin interfaceDecimalQuantity- Parameters:
multiplicand- The value by which to multiply.
-
negate
public void negate()Description copied from interface:DecimalQuantityFlips the sign from positive to negative and back.- Specified by:
negatein interfaceDecimalQuantity
-
getMagnitude
- Specified by:
getMagnitudein interfaceDecimalQuantity- Returns:
- The power of ten corresponding to the most significant nonzero digit.
- Throws:
ArithmeticException- If the value represented is zero.
-
adjustMagnitude
public void adjustMagnitude(int delta) Description copied from interface:DecimalQuantityScales the number by a power of ten. For example, if the value is currently "1234.56", calling this method with delta=-3 will change the value to "1.23456".- Specified by:
adjustMagnitudein interfaceDecimalQuantity- Parameters:
delta- The number of magnitudes of ten to change by.
-
getExponent
public int getExponent()- Specified by:
getExponentin interfaceDecimalQuantity- Returns:
- The value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific). Ex: given the number 1000 as "1K" / "1E3", the return value will be 3 (positive).
-
adjustExponent
public void adjustExponent(int delta) Description copied from interface:DecimalQuantityAdjusts the value for the (suppressed) exponent stored when using notation with exponents (ex: compact, scientific).Adjusting the exponent is decoupled from
DecimalQuantity.adjustMagnitude(int)in order to allow flexibility forStandardPluralto be selected in formatting (ex: for compact notation) either with or without the exponent applied in the value of the number.- Specified by:
adjustExponentin interfaceDecimalQuantity- Parameters:
delta- The value to adjust the exponent by.
-
resetExponent
public void resetExponent()Description copied from interface:DecimalQuantityResets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.- Specified by:
resetExponentin interfaceDecimalQuantity
-
isHasIntegerValue
public boolean isHasIntegerValue()Description copied from interface:PluralRules.IFixedDecimalWhether the number has no nonzero fraction digits.- Specified by:
isHasIntegerValuein interfacePluralRules.IFixedDecimal
-
getStandardPlural
Description copied from interface:DecimalQuantityComputes the plural form for this number based on the specified set of rules.- Specified by:
getStandardPluralin interfaceDecimalQuantity- Parameters:
rules- APluralRulesobject representing the set of rules.- Returns:
- The
StandardPluralaccording to the PluralRules. If the plural form is not in the set of standard plurals,StandardPlural.OTHERis returned instead.
-
getPluralOperand
Description copied from interface:PluralRules.IFixedDecimalReturns the value corresponding to the specified operand (n, i, f, t, v, or w). If the operand is 'n', returns a double; otherwise, returns an integer.- Specified by:
getPluralOperandin interfacePluralRules.IFixedDecimal
-
populateUFieldPosition
Description copied from interface:DecimalQuantityIf the givenFieldPositionis aUFieldPosition, populates it with the fraction length and fraction long value. If the argument is not aUFieldPosition, nothing happens.- Specified by:
populateUFieldPositionin interfaceDecimalQuantity- Parameters:
fp- TheUFieldPositionto populate.
-
getUpperDisplayMagnitude
public int getUpperDisplayMagnitude()Description copied from interface:DecimalQuantityGets the largest power of ten that needs to be displayed. The value returned by this function will be bounded between minInt and maxInt.- Specified by:
getUpperDisplayMagnitudein interfaceDecimalQuantity- Returns:
- The highest-magnitude digit to be displayed.
-
getLowerDisplayMagnitude
public int getLowerDisplayMagnitude()Description copied from interface:DecimalQuantityGets the smallest power of ten that needs to be displayed. The value returned by this function will be bounded between -minFrac and -maxFrac.- Specified by:
getLowerDisplayMagnitudein interfaceDecimalQuantity- Returns:
- The lowest-magnitude digit to be displayed.
-
getDigit
public byte getDigit(int magnitude) Description copied from interface:DecimalQuantityGets the digit at the specified magnitude. For example, if the represented number is 12.3, getDigit(-1) returns 3, since 3 is the digit corresponding to 10^-1.- Specified by:
getDigitin interfaceDecimalQuantity- Parameters:
magnitude- The magnitude of the digit.- Returns:
- The digit at the specified magnitude.
-
isNegative
public boolean isNegative()- Specified by:
isNegativein interfaceDecimalQuantity- Returns:
- Whether the value represented by this
DecimalQuantityis less than zero.
-
signum
- Specified by:
signumin interfaceDecimalQuantity- Returns:
- The appropriate value from the Signum enum.
-
isInfinite
public boolean isInfinite()- Specified by:
isInfinitein interfaceDecimalQuantity- Specified by:
isInfinitein interfacePluralRules.IFixedDecimal- Returns:
- Whether the value represented by this
DecimalQuantityis infinite.
-
isNaN
public boolean isNaN()- Specified by:
isNaNin interfaceDecimalQuantity- Specified by:
isNaNin interfacePluralRules.IFixedDecimal- Returns:
- Whether the value represented by this
DecimalQuantityis not a number.
-
isZeroish
public boolean isZeroish()- Specified by:
isZeroishin interfaceDecimalQuantity- Returns:
- Whether the value represented by this
DecimalQuantityis zero, infinity, or NaN.
-
setToInt
public void setToInt(int n) -
setToLong
public void setToLong(long n) -
setToBigInteger
-
setToDouble
public void setToDouble(double n) Sets the internal BCD state to represent the value in the given double.- Parameters:
n- The value to consume.
-
setToBigDecimal
Sets the internal BCD state to represent the value in the given BigDecimal.- Specified by:
setToBigDecimalin interfaceDecimalQuantity- Parameters:
n- The value to consume.
-
toLong
public long toLong(boolean truncateIfOverflow) Description copied from interface:DecimalQuantityReturns a long approximating the decimal quantity. A long can only represent the integral part of the number. Note: this method incorporates the value ofgetExponent(for cases such as compact notation) to return the proper long value represented by the result.- Specified by:
toLongin interfaceDecimalQuantity- Parameters:
truncateIfOverflow- if false and the number does NOT fit, fails with an error. See comment about call site guards in DecimalQuantity_AbstractBCD.java- Returns:
- A 64-bit integer representation of the internal number.
-
toFractionLong
public long toFractionLong(boolean includeTrailingZeros) This returns a long representing the fraction digits of the number, as required by PluralRules. For example, if we represent the number "1.20" (including optional and required digits), then this function returns "20" if includeTrailingZeros is true or "2" if false. Note: this method incorporates the value ofexponent(for cases such as compact notation) to return the proper long value represented by the result. -
fitsInLong
public boolean fitsInLong()Returns whether or not a Long can fully represent the value stored in this DecimalQuantity. -
toDouble
public double toDouble()Returns a double approximating the internal BCD. The double may not retain all of the information encoded in the BCD if the BCD represents a number out of range of a double.- Specified by:
toDoublein interfaceDecimalQuantity- Returns:
- A double representation of the internal BCD.
-
toBigDecimal
- Specified by:
toBigDecimalin interfaceDecimalQuantity
-
truncate
public void truncate()Removes all fraction digits. -
roundToNickel
Description copied from interface:DecimalQuantityRounds the number to the nearest multiple of 5 at the specified magnitude. For example, when magnitude == -2, this performs rounding to the nearest 0.05.- Specified by:
roundToNickelin interfaceDecimalQuantity- Parameters:
magnitude- The magnitude at which the digit should become either 0 or 5.mathContext- Rounding strategy.
-
roundToMagnitude
Description copied from interface:DecimalQuantityRounds the number to a specified magnitude (power of ten).- Specified by:
roundToMagnitudein interfaceDecimalQuantity- Parameters:
magnitude- The power of ten to which to round. For example, a value of -2 will round to 2 decimal places.mathContext- TheMathContextto use if rounding is necessary. Undefined behavior if null.
-
roundToInfinity
public void roundToInfinity()Description copied from interface:DecimalQuantityRounds the number to an infinite number of decimal points. This has no effect except for forcing the double inDecimalQuantity_AbstractBCDto adopt its exact representation.- Specified by:
roundToInfinityin interfaceDecimalQuantity
-
appendDigit
Deprecated.This API is ICU internal only.Appends a digit, optionally with one or more leading zeros, to the end of the value represented by this DecimalQuantity.The primary use of this method is to construct numbers during a parsing loop. It allows parsing to take advantage of the digit list infrastructure primarily designed for formatting.
- Parameters:
value- The digit to append.leadingZeros- The number of zeros to append before the digit. For example, if the value in this instance starts as 12.3, and you append a 4 with 1 leading zero, the value becomes 12.304.appendAsInteger- If true, increase the magnitude of existing digits to make room for the new digit. If false, append to the end like a fraction digit. If true, there must not be any fraction digits already in the number.
-
toPlainString
Description copied from interface:DecimalQuantityReturns the string in "plain" format (no exponential notation) using ASCII digits.- Specified by:
toPlainStringin interfaceDecimalQuantity
-
toPlainString
-
toScientificString
-
toScientificString
-
toExponentString
Description copied from interface:DecimalQuantityReturns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples.- Specified by:
toExponentStringin interfaceDecimalQuantity
-
equals
-
getDigitPos
protected abstract byte getDigitPos(int position) Returns a single digit from the BCD list. No internal state is changed by calling this method.- Parameters:
position- The position of the digit to pop, counted in BCD units from the least significant digit. If outside the range supported by the implementation, zero is returned.- Returns:
- The digit at the specified location.
-
setDigitPos
protected abstract void setDigitPos(int position, byte value) Sets the digit in the BCD list. This method only sets the digit; it is the caller's responsibility to callcompact()after setting the digit, and to ensure that the precision field is updated to reflect the correct number of digits if a nonzero digit is added to the decimal.- Parameters:
position- The position of the digit to pop, counted in BCD units from the least significant digit. If outside the range supported by the implementation, an AssertionError is thrown.value- The digit to set at the specified location.
-
shiftLeft
protected abstract void shiftLeft(int numDigits) Adds zeros to the end of the BCD list. This will result in an invalid BCD representation; it is the caller's responsibility to do further manipulation and then callcompact().- Parameters:
numDigits- The number of zeros to add.
-
shiftRight
protected abstract void shiftRight(int numDigits) Removes digits from the end of the BCD list. This may result in an invalid BCD representation; it is the caller's responsibility to follow-up with a call tocompact().- Parameters:
numDigits- The number of digits to remove.
-
popFromLeft
protected abstract void popFromLeft(int numDigits) Directly removes digits from the front of the BCD list. Updates precision. CAUTION: it is the caller's responsibility to callcompact()after this method. -
setBcdToZero
protected abstract void setBcdToZero()Sets the internal representation to zero. Clears any values stored in scale, precision, hasDouble, origDouble, origDelta, exponent, and BCD data. -
readIntToBcd
protected abstract void readIntToBcd(int input) Sets the internal BCD state to represent the value in the given int. The int is guaranteed to be either positive. The internal state is guaranteed to be empty when this method is called.- Parameters:
n- The value to consume.
-
readLongToBcd
protected abstract void readLongToBcd(long input) Sets the internal BCD state to represent the value in the given long. The long is guaranteed to be either positive. The internal state is guaranteed to be empty when this method is called.- Parameters:
n- The value to consume.
-
readBigIntegerToBcd
Sets the internal BCD state to represent the value in the given BigInteger. The BigInteger is guaranteed to be positive, and it is guaranteed to be larger than Long.MAX_VALUE. The internal state is guaranteed to be empty when this method is called.- Parameters:
n- The value to consume.
-
bcdToBigDecimal
Returns a BigDecimal encoding the internal BCD value.- Returns:
- A BigDecimal representation of the internal BCD.
-
copyBcdFrom
-
compact
protected abstract void compact()Removes trailing zeros from the BCD (adjusting the scale as required) and then computes the precision. The precision is the number of digits in the number up through the greatest nonzero digit.This method must always be called when bcd changes in order for assumptions to be correct in methods like
fractionCount().
-