Class DecimalQuantity_AbstractBCD
- All Implemented Interfaces:
DecimalQuantity
,PluralRules.IFixedDecimal
- Direct Known Subclasses:
DecimalQuantity_DualStorageBCD
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double[]
boolean
Deprecated.This API is ICU internal only.protected int
The value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific).protected byte
A bitmask of properties relating to the number represented by this object.protected static final int
(package private) static final byte[]
protected boolean
Whether the value in the BCD comes from the double fast path without having been rounded to ensure correctnessprotected int
protected static final int
protected static final int
protected int
The change in magnitude relative to the original double.protected double
The original number provided by the user and which is represented in BCD.protected int
The number of digits in the BCD.protected int
protected int
The power of ten corresponding to the least significant digit in the BCD.private static final int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
private void
private void
_setToDoubleFast
(double n) Uses double multiplication and division to get the number into integer space before converting to digits.private void
_setToInt
(int n) private void
_setToLong
(long n) void
adjustExponent
(int delta) Adjusts the value for the (suppressed) exponent stored when using notation with exponents (ex: compact, scientific).void
adjustMagnitude
(int delta) Scales the number by a power of ten.void
appendDigit
(byte value, int leadingZeros, boolean appendAsInteger) Deprecated.This API is ICU internal only.void
applyMaxInteger
(int maxInt) Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.protected abstract BigDecimal
Returns a BigDecimal encoding the internal BCD value.clear()
protected abstract void
compact()
Removes trailing zeros from the BCD (adjusting the scale as required) and then computes the precision.private void
Uses Double.toString() to obtain an exact accurate representation of the double, overwriting it into the BCD.protected abstract void
copyBcdFrom
(DecimalQuantity _other) void
copyFrom
(DecimalQuantity _other) Sets this instance to be equal to another instance.boolean
boolean
Returns whether or not a Long can fully represent the value stored in this DecimalQuantity.private int
private int
byte
getDigit
(int magnitude) Gets the digit at the specified magnitude.protected abstract byte
getDigitPos
(int position) Returns a single digit from the BCD list.int
int
Gets the smallest power of ten that needs to be displayed.int
double
getPluralOperand
(PluralRules.Operand operand) Returns the value corresponding to the specified operand (n, i, f, t, v, or w).long
This method is for internal testing only.getStandardPlural
(PluralRules rules) Computes the plural form for this number based on the specified set of rules.int
Gets the largest power of ten that needs to be displayed.boolean
Whether the number has no nonzero fraction digits.boolean
boolean
isNaN()
boolean
boolean
void
multiplyBy
(BigDecimal multiplicand) Multiply the internal value.void
negate()
Flips the sign from positive to negative and back.protected abstract void
popFromLeft
(int numDigits) Directly removes digits from the front of the BCD list.void
If the givenFieldPosition
is aUFieldPosition
, populates it with the fraction length and fraction long value.protected abstract void
readBigIntegerToBcd
(BigInteger input) Sets the internal BCD state to represent the value in the given BigInteger.protected abstract void
readIntToBcd
(int input) Sets the internal BCD state to represent the value in the given int.protected abstract void
readLongToBcd
(long input) Sets the internal BCD state to represent the value in the given long.void
Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.void
roundToIncrement
(BigDecimal roundingIncrement, MathContext mathContext) Rounds the number to a specified interval, such as 0.05.void
Rounds the number to an infinite number of decimal points.void
roundToMagnitude
(int magnitude, MathContext mathContext) Rounds the number to a specified magnitude (power of ten).private void
roundToMagnitude
(int magnitude, MathContext mathContext, boolean nickel) void
roundToNickel
(int magnitude, MathContext mathContext) Rounds the number to the nearest multiple of 5 at the specified magnitude.private static int
safeSubtract
(int a, int b) protected abstract void
Sets the internal representation to zero.protected abstract void
setDigitPos
(int position, byte value) Sets the digit in the BCD list.void
setMinFraction
(int minFrac) Sets the minimum fraction digits that thisDecimalQuantity
should generate.void
setMinInteger
(int minInt) Sets the minimum integer digits that thisDecimalQuantity
should generate.void
Sets the internal BCD state to represent the value in the given BigDecimal.void
void
setToDouble
(double n) Sets the internal BCD state to represent the value in the given double.void
setToInt
(int n) void
setToLong
(long n) protected abstract void
shiftLeft
(int numDigits) Adds zeros to the end of the BCD list.protected abstract void
shiftRight
(int numDigits) Removes digits from the end of the BCD list.signum()
double
toDouble()
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.private void
toExponentString
(StringBuilder result) long
toFractionLong
(boolean includeTrailingZeros) This returns a long representing the fraction digits of the number, as required by PluralRules.long
toLong
(boolean truncateIfOverflow) Returns a long approximating the decimal quantity.Returns the string in "plain" format (no exponential notation) using ASCII digits.void
toPlainString
(StringBuilder result) void
toScientificString
(StringBuilder result) void
truncate()
Removes all fraction digits.Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface 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
#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). -
DOUBLE_MULTIPLIERS
private static final double[] DOUBLE_MULTIPLIERS -
explicitExactDouble
Deprecated.This API is ICU internal only.Whether thisDecimalQuantity_DualStorageBCD
has been explicitly converted to an exact double. true if backed by a double that was explicitly converted via convertToAccurateDouble; false otherwise. Used for testing. -
INT64_BCD
static final byte[] INT64_BCD -
SECTION_LOWER_EDGE
private static final int SECTION_LOWER_EDGE- See Also:
-
SECTION_UPPER_EDGE
private static final int SECTION_UPPER_EDGE- See Also:
-
-
Constructor Details
-
DecimalQuantity_AbstractBCD
public DecimalQuantity_AbstractBCD()
-
-
Method Details
-
copyFrom
Description copied from interface:DecimalQuantity
Sets this instance to be equal to another instance.- Specified by:
copyFrom
in interfaceDecimalQuantity
- Parameters:
_other
- The instance to copy from.
-
clear
-
setMinInteger
public void setMinInteger(int minInt) Description copied from interface:DecimalQuantity
Sets the minimum integer digits that thisDecimalQuantity
should generate. This method does not perform rounding.- Specified by:
setMinInteger
in interfaceDecimalQuantity
- Parameters:
minInt
- The minimum number of integer digits.
-
setMinFraction
public void setMinFraction(int minFrac) Description copied from interface:DecimalQuantity
Sets the minimum fraction digits that thisDecimalQuantity
should generate. This method does not perform rounding.- Specified by:
setMinFraction
in interfaceDecimalQuantity
- Parameters:
minFrac
- The minimum number of fraction digits.
-
applyMaxInteger
public void applyMaxInteger(int maxInt) Description copied from interface:DecimalQuantity
Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.- Specified by:
applyMaxInteger
in interfaceDecimalQuantity
- Parameters:
maxInt
- The maximum number of integer digits.
-
getPositionFingerprint
public long getPositionFingerprint()Description copied from interface:DecimalQuantity
This method is for internal testing only.- Specified by:
getPositionFingerprint
in interfaceDecimalQuantity
-
roundToIncrement
Description copied from interface:DecimalQuantity
Rounds 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:
roundToIncrement
in interfaceDecimalQuantity
- Parameters:
roundingIncrement
- The increment to which to round.mathContext
- TheMathContext
to use if rounding is necessary. Undefined behavior if null.
-
multiplyBy
Description copied from interface:DecimalQuantity
Multiply the internal value.- Specified by:
multiplyBy
in interfaceDecimalQuantity
- Parameters:
multiplicand
- The value by which to multiply.
-
negate
public void negate()Description copied from interface:DecimalQuantity
Flips the sign from positive to negative and back.- Specified by:
negate
in interfaceDecimalQuantity
-
getMagnitude
- Specified by:
getMagnitude
in 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:DecimalQuantity
Scales 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:
adjustMagnitude
in interfaceDecimalQuantity
- Parameters:
delta
- The number of magnitudes of ten to change by.
-
getExponent
public int getExponent()- Specified by:
getExponent
in 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:DecimalQuantity
Adjusts 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 forStandardPlural
to be selected in formatting (ex: for compact notation) either with or without the exponent applied in the value of the number.- Specified by:
adjustExponent
in interfaceDecimalQuantity
- Parameters:
delta
- The value to adjust the exponent by.
-
resetExponent
public void resetExponent()Description copied from interface:DecimalQuantity
Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.- Specified by:
resetExponent
in interfaceDecimalQuantity
-
isHasIntegerValue
public boolean isHasIntegerValue()Description copied from interface:PluralRules.IFixedDecimal
Whether the number has no nonzero fraction digits.- Specified by:
isHasIntegerValue
in interfacePluralRules.IFixedDecimal
-
getStandardPlural
Description copied from interface:DecimalQuantity
Computes the plural form for this number based on the specified set of rules.- Specified by:
getStandardPlural
in interfaceDecimalQuantity
- Parameters:
rules
- APluralRules
object representing the set of rules.- Returns:
- The
StandardPlural
according to the PluralRules. If the plural form is not in the set of standard plurals,StandardPlural.OTHER
is returned instead.
-
getPluralOperand
Description copied from interface:PluralRules.IFixedDecimal
Returns 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:
getPluralOperand
in interfacePluralRules.IFixedDecimal
-
populateUFieldPosition
Description copied from interface:DecimalQuantity
If the givenFieldPosition
is aUFieldPosition
, populates it with the fraction length and fraction long value. If the argument is not aUFieldPosition
, nothing happens.- Specified by:
populateUFieldPosition
in interfaceDecimalQuantity
- Parameters:
fp
- TheUFieldPosition
to populate.
-
getUpperDisplayMagnitude
public int getUpperDisplayMagnitude()Description copied from interface:DecimalQuantity
Gets 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:
getUpperDisplayMagnitude
in interfaceDecimalQuantity
- Returns:
- The highest-magnitude digit to be displayed.
-
getLowerDisplayMagnitude
public int getLowerDisplayMagnitude()Description copied from interface:DecimalQuantity
Gets 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:
getLowerDisplayMagnitude
in interfaceDecimalQuantity
- Returns:
- The lowest-magnitude digit to be displayed.
-
getDigit
public byte getDigit(int magnitude) Description copied from interface:DecimalQuantity
Gets 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:
getDigit
in interfaceDecimalQuantity
- Parameters:
magnitude
- The magnitude of the digit.- Returns:
- The digit at the specified magnitude.
-
fractionCount
private int fractionCount() -
fractionCountWithoutTrailingZeros
private int fractionCountWithoutTrailingZeros() -
isNegative
public boolean isNegative()- Specified by:
isNegative
in interfaceDecimalQuantity
- Returns:
- Whether the value represented by this
DecimalQuantity
is less than zero.
-
signum
- Specified by:
signum
in interfaceDecimalQuantity
- Returns:
- The appropriate value from the Signum enum.
-
isInfinite
public boolean isInfinite()- Specified by:
isInfinite
in interfaceDecimalQuantity
- Specified by:
isInfinite
in interfacePluralRules.IFixedDecimal
- Returns:
- Whether the value represented by this
DecimalQuantity
is infinite.
-
isNaN
public boolean isNaN()- Specified by:
isNaN
in interfaceDecimalQuantity
- Specified by:
isNaN
in interfacePluralRules.IFixedDecimal
- Returns:
- Whether the value represented by this
DecimalQuantity
is not a number.
-
isZeroish
public boolean isZeroish()- Specified by:
isZeroish
in interfaceDecimalQuantity
- Returns:
- Whether the value represented by this
DecimalQuantity
is zero, infinity, or NaN.
-
setToInt
public void setToInt(int n) -
_setToInt
private void _setToInt(int n) -
setToLong
public void setToLong(long n) -
_setToLong
private void _setToLong(long n) -
setToBigInteger
-
_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.
-
_setToDoubleFast
private void _setToDoubleFast(double n) Uses double multiplication and division to get the number into integer space before converting to digits. Since double arithmetic is inexact, the resulting digits may not be accurate. -
convertToAccurateDouble
private void convertToAccurateDouble()Uses Double.toString() to obtain an exact accurate representation of the double, overwriting it into the BCD. This method can be called at any point after_setToDoubleFast(double)
whileisApproximate
is still true. -
setToBigDecimal
Sets the internal BCD state to represent the value in the given BigDecimal.- Specified by:
setToBigDecimal
in interfaceDecimalQuantity
- Parameters:
n
- The value to consume.
-
_setToBigDecimal
-
toLong
public long toLong(boolean truncateIfOverflow) Description copied from interface:DecimalQuantity
Returns 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:
toLong
in 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:
toDouble
in interfaceDecimalQuantity
- Returns:
- A double representation of the internal BCD.
-
toBigDecimal
- Specified by:
toBigDecimal
in interfaceDecimalQuantity
-
safeSubtract
private static int safeSubtract(int a, int b) -
truncate
public void truncate()Removes all fraction digits. -
roundToNickel
Description copied from interface:DecimalQuantity
Rounds 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:
roundToNickel
in interfaceDecimalQuantity
- Parameters:
magnitude
- The magnitude at which the digit should become either 0 or 5.mathContext
- Rounding strategy.
-
roundToMagnitude
Description copied from interface:DecimalQuantity
Rounds the number to a specified magnitude (power of ten).- Specified by:
roundToMagnitude
in interfaceDecimalQuantity
- Parameters:
magnitude
- The power of ten to which to round. For example, a value of -2 will round to 2 decimal places.mathContext
- TheMathContext
to use if rounding is necessary. Undefined behavior if null.
-
roundToMagnitude
-
roundToInfinity
public void roundToInfinity()Description copied from interface:DecimalQuantity
Rounds the number to an infinite number of decimal points. This has no effect except for forcing the double inDecimalQuantity_AbstractBCD
to adopt its exact representation.- Specified by:
roundToInfinity
in 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:DecimalQuantity
Returns the string in "plain" format (no exponential notation) using ASCII digits.- Specified by:
toPlainString
in interfaceDecimalQuantity
-
toPlainString
-
toScientificString
-
toScientificString
-
toExponentString
Description copied from interface:DecimalQuantity
Returns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples.- Specified by:
toExponentString
in interfaceDecimalQuantity
-
toExponentString
-
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()
.
-