Package com.squarespace.cldrengine.api
Class Decimal
- java.lang.Object
-
- com.squarespace.cldrengine.api.Decimal
-
public class Decimal extends java.lang.Object
Arbitrary precision decimal type.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Decimal.Properties
static class
Decimal.Scientific
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_increment()
Increment the least-significant digit of the coefficient.protected void
_setScale(int scale, RoundingModeType mode)
protected void
_shiftleft(int shift)
Mutating in-place shift left.protected void
_shiftright(int shift)
Mutating in-place shift right.protected void
_shiftright(int shift, RoundingModeType mode)
Mutating in-place shift right.protected void
_stripTrailingZeros()
Decimal
abs()
Return the absolute value of the number.Decimal
add(Decimal v)
Adds v.protected Decimal
addsub(Decimal u, Decimal v, int vsign)
int
alignexp()
Adjusted exponent for alignment.int
compare(Decimal v)
int
compare(Decimal v, boolean abs)
Compare decimal u to v, returning the following: -1 if u < v 0 if u = v 1 if u > v If the abs flag is true compare the absolute values.Decimal
decrement()
Decrement the least-significant integer digit.Decimal
divide(Decimal v, MathContext context)
Divide by v with optional math context.Pair<Decimal,Decimal>
divmod(Decimal v)
Divide by v and return the quotient and remainder.boolean
equals(java.lang.Object other)
int
exp()
<R> void
format(DecimalFormatter<R> formatter, java.lang.String decimal, java.lang.String group, int minInt, int minGroup, int priGroup, int secGroup, boolean zeroScale, java.lang.String[] digits)
Low-level formatting of string and Part[] forms.protected java.lang.String
formatFlags()
protected java.lang.String
formatString(Decimal d, int minInt)
protected Decimal
handleFlags(com.squarespace.cldrengine.api.Decimal.Op op, Decimal v)
Handle setting of flags for operations per the IEEE-754-2008 specification.Decimal
increment()
Increment the least-significant integer digit.int
integerDigits()
Number of integer digits, 1 or higher.boolean
isFinite()
boolean
isInfinity()
boolean
isInteger()
Check if this number can be represented as an integer without loss of precision.boolean
isNaN()
boolean
isNegative()
Indicates this number is negative.protected boolean
isodd()
Return true of this instance is odd.boolean
isZero()
Number is exactly zero.Decimal
mod(Decimal v)
Divide by v and return the remainder.Decimal
movePoint(int n)
Move the decimal point -n (left) or +n (right) places.Decimal
multiply(Decimal v, MathContext context)
Multplies by v with optional math context.Decimal
negate()
Invert this number's sign.int
precision()
Number of digits in the unscaled value.Decimal.Properties
properties()
Return the raw internal properties of the number.protected boolean
round(long rnd, long rest, RoundingModeType mode)
Return a rounding indicator for a given rounding mode.int
scale()
Scale is the number of digits to the right of the decimal point.Decimal.Scientific
scientific(int minIntDigits)
Return a scientific representation of the number, Decimal coefficient and adjusted exponent.Decimal
setScale(int scale)
Returns a new number with the given scale, shifting the coefficient as needed.Decimal
setScale(int scale, RoundingModeType mode)
Returns a new number with the given scale, shifting the coefficient as needed.Decimal
shiftleft(int shift)
Shifts all digits to the left, increasing the precision.Decimal
shiftright(int shift, RoundingModeType mode)
Shifts all digits to the right, reducing the precision.int
signum()
Signum.Decimal
stripTrailingZeros()
Strip all trailing zeros.Decimal
subtract(Decimal v)
Subtracts v.java.lang.String
toString()
Format the number to a string, using fixed point.int
trailingZeros()
Number of trailing zeros.protected Decimal
trim()
Trim leading zeros from a result and reset sign and exponent accordingly.protected void
trimLeadingZeros()
-
-
-
Constructor Detail
-
Decimal
public Decimal(java.lang.String s)
-
Decimal
public Decimal(Decimal d)
-
Decimal
public Decimal(int n)
-
Decimal
public Decimal(long n)
-
Decimal
public Decimal(float n)
-
Decimal
public Decimal(double n)
-
Decimal
public Decimal(int sign, int exp, long[] data, int flag)
-
-
Method Detail
-
exp
public int exp()
-
isNaN
public boolean isNaN()
-
isFinite
public boolean isFinite()
-
isInfinity
public boolean isInfinity()
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
compare
public int compare(Decimal v)
-
compare
public int compare(Decimal v, boolean abs)
Compare decimal u to v, returning the following: -1 if u < v 0 if u = v 1 if u > v If the abs flag is true compare the absolute values. Any NAN argument will always return -1.
-
properties
public Decimal.Properties properties()
Return the raw internal properties of the number. Use with caution.
-
abs
public Decimal abs()
Return the absolute value of the number.
-
negate
public Decimal negate()
Invert this number's sign.
-
isNegative
public boolean isNegative()
Indicates this number is negative.
-
signum
public int signum()
Signum.
-
isInteger
public boolean isInteger()
Check if this number can be represented as an integer without loss of precision. For example, '12.000' is the same number as '12'.
-
isZero
public boolean isZero()
Number is exactly zero. Exponent may exist, e.g. "0e-2" os "0.00"
-
multiply
public Decimal multiply(Decimal v, MathContext context)
Multplies by v with optional math context.
-
divide
public Decimal divide(Decimal v, MathContext context)
Divide by v with optional math context.
-
divmod
public Pair<Decimal,Decimal> divmod(Decimal v)
Divide by v and return the quotient and remainder.
-
trailingZeros
public int trailingZeros()
Number of trailing zeros.
-
stripTrailingZeros
public Decimal stripTrailingZeros()
Strip all trailing zeros.
-
scientific
public Decimal.Scientific scientific(int minIntDigits)
Return a scientific representation of the number, Decimal coefficient and adjusted exponent.
-
precision
public int precision()
Number of digits in the unscaled value.
-
scale
public int scale()
Scale is the number of digits to the right of the decimal point.
-
integerDigits
public int integerDigits()
Number of integer digits, 1 or higher.
-
setScale
public Decimal setScale(int scale)
Returns a new number with the given scale, shifting the coefficient as needed.
-
setScale
public Decimal setScale(int scale, RoundingModeType mode)
Returns a new number with the given scale, shifting the coefficient as needed.
-
alignexp
public int alignexp()
Adjusted exponent for alignment. Two numbers with the same aligned exponent are aligned for arithmetic operations. If the aligned exponents do not match one number must be shifted.
-
movePoint
public Decimal movePoint(int n)
Move the decimal point -n (left) or +n (right) places. Does not change precision, only affects the exponent.
-
shiftleft
public Decimal shiftleft(int shift)
Shifts all digits to the left, increasing the precision.
-
shiftright
public Decimal shiftright(int shift, RoundingModeType mode)
Shifts all digits to the right, reducing the precision. Result is rounded using the given rounding mode.
-
increment
public Decimal increment()
Increment the least-significant integer digit.
-
decrement
public Decimal decrement()
Decrement the least-significant integer digit.
-
toString
public java.lang.String toString()
Format the number to a string, using fixed point.- Overrides:
toString
in classjava.lang.Object
-
format
public <R> void format(DecimalFormatter<R> formatter, java.lang.String decimal, java.lang.String group, int minInt, int minGroup, int priGroup, int secGroup, boolean zeroScale, java.lang.String[] digits)
Low-level formatting of string and Part[] forms.
-
formatFlags
protected java.lang.String formatFlags()
-
formatString
protected java.lang.String formatString(Decimal d, int minInt)
-
handleFlags
protected Decimal handleFlags(com.squarespace.cldrengine.api.Decimal.Op op, Decimal v)
Handle setting of flags for operations per the IEEE-754-2008 specification. These rules are also referenced in the EcmaScript specification: 12.7.3.1 - Applying the mul operator: https://tc39.github.io/ecma262/#sec-applying-the-mul-operator 12.7.3.2 - Applying the div operator: https://tc39.github.io/ecma262/#sec-applying-the-div-operator 12.7.3.3 - Applying the mod operator: https://tc39.github.io/ecma262/#sec-applying-the-mod-operator 12.8.5 - Applying the additive operators to numbers: https://tc39.github.io/ecma262/#sec-applying-the-additive-operators-to-numbers
-
_shiftleft
protected void _shiftleft(int shift)
Mutating in-place shift left.
-
_shiftright
protected void _shiftright(int shift)
Mutating in-place shift right.
-
_shiftright
protected void _shiftright(int shift, RoundingModeType mode)
Mutating in-place shift right.
-
_setScale
protected void _setScale(int scale, RoundingModeType mode)
-
_stripTrailingZeros
protected void _stripTrailingZeros()
-
trim
protected Decimal trim()
Trim leading zeros from a result and reset sign and exponent accordingly.
-
trimLeadingZeros
protected void trimLeadingZeros()
-
_increment
protected void _increment()
Increment the least-significant digit of the coefficient.
-
round
protected boolean round(long rnd, long rest, RoundingModeType mode)
Return a rounding indicator for a given rounding mode.
-
isodd
protected boolean isodd()
Return true of this instance is odd.
-
-