Package com.ibm.icu.impl.number
Interface DecimalQuantity
- All Superinterfaces:
PluralRules.IFixedDecimal
- All Known Implementing Classes:
DecimalQuantity_AbstractBCD
,DecimalQuantity_DualStorageBCD
An interface representing a number to be processed by the decimal formatting pipeline. Includes
methods for rounding, plural rules, and decimal digit extraction.
By design, this is NOT IMMUTABLE and NOT THREAD SAFE. It is intended to be an intermediate object holding state during a pass through the decimal formatting pipeline.
Implementations of this interface are free to use any internal storage mechanism.
TODO: Should I change this to an abstract class so that logic for min/max digits doesn't need to be copied to every implementation?
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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
applyMaxInteger
(int maxInt) Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.void
copyFrom
(DecimalQuantity other) Sets this instance to be equal to another instance.Like clone, but without the restrictions of the Cloneable interface clone.byte
getDigit
(int magnitude) Gets the digit at the specified magnitude.int
int
Gets the smallest power of ten that needs to be displayed.int
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
boolean
isNaN()
boolean
boolean
int
void
multiplyBy
(BigDecimal multiplicand) Multiply the internal value.void
negate()
Flips the sign from positive to negative and back.void
If the givenFieldPosition
is aUFieldPosition
, populates it with the fraction length and fraction long value.void
Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.void
roundToIncrement
(BigDecimal roundingInterval, 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 roundingMagnitude, MathContext mathContext) Rounds the number to a specified magnitude (power of ten).void
roundToNickel
(int magnitude, MathContext mathContext) Rounds the number to the nearest multiple of 5 at the specified magnitude.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
setToBigDecimal
(BigDecimal input) signum()
double
toDouble()
Returns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples.long
toLong
(boolean truncateIfOverflow) Returns a long approximating the decimal quantity.Returns the string in "plain" format (no exponential notation) using ASCII digits.Methods inherited from interface com.ibm.icu.text.PluralRules.IFixedDecimal
getPluralOperand, isHasIntegerValue
-
Method Details
-
setMinInteger
void setMinInteger(int minInt) Sets the minimum integer digits that thisDecimalQuantity
should generate. This method does not perform rounding.- Parameters:
minInt
- The minimum number of integer digits.
-
setMinFraction
void setMinFraction(int minFrac) Sets the minimum fraction digits that thisDecimalQuantity
should generate. This method does not perform rounding.- Parameters:
minFrac
- The minimum number of fraction digits.
-
applyMaxInteger
void applyMaxInteger(int maxInt) Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.- Parameters:
maxInt
- The maximum number of integer digits.
-
roundToIncrement
Rounds the number to a specified interval, such as 0.05.If rounding to a power of ten, use the more efficient
roundToMagnitude(int, java.math.MathContext)
instead.- Parameters:
roundingInterval
- The increment to which to round.mathContext
- TheMathContext
to use if rounding is necessary. Undefined behavior if null.
-
roundToNickel
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.- Parameters:
magnitude
- The magnitude at which the digit should become either 0 or 5.mathContext
- Rounding strategy.
-
roundToMagnitude
Rounds the number to a specified magnitude (power of ten).- Parameters:
roundingMagnitude
- 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.
-
roundToInfinity
void roundToInfinity()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. -
multiplyBy
Multiply the internal value.- Parameters:
multiplicand
- The value by which to multiply.
-
negate
void negate()Flips the sign from positive to negative and back. -
adjustMagnitude
void adjustMagnitude(int delta) 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".- Parameters:
delta
- The number of magnitudes of ten to change by.
-
getMagnitude
- Returns:
- The power of ten corresponding to the most significant nonzero digit.
- Throws:
ArithmeticException
- If the value represented is zero.
-
getExponent
int getExponent()- 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
void adjustExponent(int delta) Adjusts the value for the (suppressed) exponent stored when using notation with exponents (ex: compact, scientific).Adjusting the exponent is decoupled from
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.- Parameters:
delta
- The value to adjust the exponent by.
-
resetExponent
void resetExponent()Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent. -
isZeroish
boolean isZeroish()- Returns:
- Whether the value represented by this
DecimalQuantity
is zero, infinity, or NaN.
-
isNegative
boolean isNegative()- Returns:
- Whether the value represented by this
DecimalQuantity
is less than zero.
-
signum
Modifier.Signum signum()- Returns:
- The appropriate value from the Signum enum.
-
isInfinite
boolean isInfinite()- Specified by:
isInfinite
in interfacePluralRules.IFixedDecimal
- Returns:
- Whether the value represented by this
DecimalQuantity
is infinite.
-
isNaN
boolean isNaN()- Specified by:
isNaN
in interfacePluralRules.IFixedDecimal
- Returns:
- Whether the value represented by this
DecimalQuantity
is not a number.
-
toDouble
double toDouble()- Returns:
- The value contained in this
DecimalQuantity
approximated as a double.
-
toBigDecimal
BigDecimal toBigDecimal() -
toLong
long toLong(boolean truncateIfOverflow) 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.- 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.
-
setToBigDecimal
-
maxRepresentableDigits
int maxRepresentableDigits() -
getStandardPlural
Computes the plural form for this number based on the specified set of rules.- 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.
-
getDigit
byte getDigit(int magnitude) 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.- Parameters:
magnitude
- The magnitude of the digit.- Returns:
- The digit at the specified magnitude.
-
getUpperDisplayMagnitude
int getUpperDisplayMagnitude()Gets the largest power of ten that needs to be displayed. The value returned by this function will be bounded between minInt and maxInt.- Returns:
- The highest-magnitude digit to be displayed.
-
getLowerDisplayMagnitude
int getLowerDisplayMagnitude()Gets the smallest power of ten that needs to be displayed. The value returned by this function will be bounded between -minFrac and -maxFrac.- Returns:
- The lowest-magnitude digit to be displayed.
-
toPlainString
String toPlainString()Returns the string in "plain" format (no exponential notation) using ASCII digits. -
toExponentString
String toExponentString()Returns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples. -
createCopy
DecimalQuantity createCopy()Like clone, but without the restrictions of the Cloneable interface clone.- Returns:
- A copy of this instance which can be mutated without affecting this instance.
-
copyFrom
Sets this instance to be equal to another instance.- Parameters:
other
- The instance to copy from.
-
getPositionFingerprint
long getPositionFingerprint()This method is for internal testing only. -
populateUFieldPosition
If the givenFieldPosition
is aUFieldPosition
, populates it with the fraction length and fraction long value. If the argument is not aUFieldPosition
, nothing happens.- Parameters:
fp
- TheUFieldPosition
to populate.
-