public interface DecimalArithmetic
Decimal
numbers for one particular
combination of scale
, rounding mode
and overflow mode
. Primitive here means that Decimal values are simply represented by their underlying unscaled
long value. All operations therefore use unscaled longs for Decimal arguments and return longs for
Decimal number results.
Application code does not usually need to use DecimalArithmetic directly. It may be appropriate however for very specialized applications with low latency, high frequency or zero garbage requirements. All operations of DecimalArithmetic do not allocate any objects (zero garbage) unless otherwise indicated.
Modifier and Type | Method and Description |
---|---|
long |
abs(long uDecimal)
Returns an unscaled decimal whose value is
abs(uDecimal) , which is the value itself if
uDecimal>=0 and -uDecimal if the given value is negative. |
long |
add(long uDecimal1,
long uDecimal2)
Returns an unscaled decimal whose value is
(uDecimal1 + uDecimal2) . |
long |
addLong(long uDecimal,
long lValue)
Returns an unscaled decimal whose value is the sum of the specified arguments:
(uDecimal + lValue) . |
long |
addUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal + unscaled * 10-scale).
|
long |
avg(long uDecimal1,
long uDecimal2)
Returns an unscaled decimal whose value is the average of
uDecimal1 and uDecimal2 . |
int |
compare(long uDecimal1,
long uDecimal2)
Compares two unscaled decimal values numerically.
|
int |
compareToUnscaled(long uDecimal,
long unscaled,
int scale)
Compares two unscaled decimal values numerically.
|
DecimalArithmetic |
deriveArithmetic(int scale)
Derives an arithmetic instance for the specified
scale using this arithmetic's rounding mode and overflow mode . |
DecimalArithmetic |
deriveArithmetic(OverflowMode overflowMode)
Derives an arithmetic instance for the same
scale as this arithmetic but for the specified
overflowMode . |
DecimalArithmetic |
deriveArithmetic(RoundingMode roundingMode)
Derives an arithmetic instance for the same
scale as this arithmetic but for the specified
roundingMode . |
DecimalArithmetic |
deriveArithmetic(RoundingMode roundingMode,
OverflowMode overflowMode)
Derives an arithmetic instance for the same
scale as this arithmetic but for the specified
roundingMode and overflowMode . |
DecimalArithmetic |
deriveArithmetic(TruncationPolicy truncationPolicy)
Derives an arithmetic instance for the same
scale as this arithmetic but with rounding and
overflow mode specified by the given truncationPolicy . |
long |
divide(long uDecimalDividend,
long uDecimalDivisor)
Returns an unscaled decimal whose value is the quotient of the specified arguments:
(uDecimalDividend / uDecimalDivisor) . |
long |
divideByLong(long uDecimalDividend,
long lDivisor)
Returns an unscaled decimal whose value is
(uDecimalDividend / lDivisor) where the second argument is a
true long value instead of an unscaled decimal. |
long |
divideByPowerOf10(long uDecimal,
int n)
Returns an unscaled decimal whose value is (uDecimal / 10n).
|
long |
divideByUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal / (unscaled * 10-scale)).
|
long |
fromBigDecimal(BigDecimal value)
Converts the specified
BigDecimal value to an unscaled decimal. |
long |
fromBigInteger(BigInteger value)
Converts the specified
BigInteger value to an unscaled decimal. |
long |
fromDouble(double value)
Converts the specified double value to an unscaled decimal.
|
long |
fromFloat(float value)
Converts the specified float value to an unscaled decimal.
|
long |
fromLong(long value)
Converts the specified long value to an unscaled decimal.
|
long |
fromUnscaled(long unscaledValue,
int scale)
Converts the specified unscaled decimal with the given scale to another unscaled decimal of the scale of this
arithmetic.
|
OverflowMode |
getOverflowMode()
Returns the overflow mode applied to operations of this
DecimalArithmetic object if an overflow
occurs. |
RoundingMode |
getRoundingMode()
Returns the rounding mode applied to operations of this
DecimalArithmetic object if rounding is
necessary. |
int |
getScale()
Returns the scale
f applied to all unscaled decimal values passed to and returned by this
DecimalArithmetic . |
ScaleMetrics |
getScaleMetrics()
Returns the scale metrics associated with this decimal arithmetic object.
|
TruncationPolicy |
getTruncationPolicy()
Returns the truncation policy defining how to handle truncation due to overflow or rounding.
|
long |
invert(long uDecimal)
Returns an unscaled decimal whose value is the inverse of the argument:
1/uDecimal . |
long |
multiply(long uDecimal1,
long uDecimal2)
Returns an unscaled decimal whose value is the product of the specified arguments:
(uDecimal1 * uDecimal2) . |
long |
multiplyByLong(long uDecimal,
long lValue)
Returns an unscaled decimal whose value is
(uDecimal * lValue) where the second argument is a true long
value instead of an unscaled decimal. |
long |
multiplyByPowerOf10(long uDecimal,
int n)
Returns an unscaled decimal whose value is (uDecimal * 10n).
|
long |
multiplyByUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal * unscaled * 10-scale).
|
long |
negate(long uDecimal)
Returns an unscaled decimal whose value is
-uDecimal . |
long |
one()
Returns the unscaled decimal for the decimal value
1 . |
long |
parse(CharSequence value,
int start,
int end)
Translates the string representation of a
Decimal into an unscaled Decimal. |
long |
parse(String value)
Translates the string representation of a
Decimal into an unscaled Decimal. |
long |
pow(long uDecimalBase,
int exponent)
Returns an unscaled decimal whose value is (uDecimalBaseexponent).
|
long |
round(long uDecimal,
int precision)
Returns an unscaled decimal whose value is rounded to the specified
precision using the
rounding mode of this arithmetic. |
long |
shiftLeft(long uDecimal,
int n)
Returns an unscaled decimal whose value is
(uDecimal << n) . |
long |
shiftRight(long uDecimal,
int n)
Returns an unscaled decimal whose value is
(uDecimal >> n) . |
int |
signum(long uDecimal)
Returns the signum function of the specified unscaled decimal.
|
long |
sqrt(long uDecimal)
Returns an unscaled decimal whose value is the square root of the specified argument: sqrt(uDecimal).
|
long |
square(long uDecimal)
Returns an unscaled decimal whose value is the square of the specified argument: uDecimal2.
|
long |
subtract(long uDecimalMinuend,
long uDecimalSubtrahend)
Returns an unscaled decimal whose value is
(uDecimalMinuend -
uDecimalSubtrahend) . |
long |
subtractLong(long uDecimal,
long lValue)
Returns an unscaled decimal whose value is the difference of the specified arguments:
(uDecimal - lValue)
. |
long |
subtractUnscaled(long uDecimal,
long unscaled,
int scale)
Returns an unscaled decimal whose value is (uDecimal - unscaled * 10-scale).
|
BigDecimal |
toBigDecimal(long uDecimal)
Converts the specified unscaled decimal value into a
BigDecimal value using this arithmetic's
scale for the result value. |
BigDecimal |
toBigDecimal(long uDecimal,
int scale)
Converts the specified unscaled decimal value into a
BigDecimal value using the specified scale
for the result value. |
double |
toDouble(long uDecimal)
Converts the specified unscaled decimal value into a double value and returns it.
|
float |
toFloat(long uDecimal)
Converts the specified unscaled decimal value into a float value and returns it.
|
long |
toLong(long uDecimal)
Converts the specified unscaled decimal value into a long value and returns it.
|
String |
toString(long uDecimal)
Converts the specified unscaled decimal value into a
String and returns it. |
void |
toString(long uDecimal,
Appendable appendable)
Converts the specified unscaled decimal value into a
String and appends the string to the
appendable . |
long |
toUnscaled(long uDecimal,
int scale)
Converts the specified unscaled decimal value into an unscaled value of the given scale.
|
int getScale()
f
applied to all unscaled decimal values passed to and returned by this
DecimalArithmetic
. Corresponds to the number of digits to the right of the decimal point (cannot be
negative).
A given Decimal
value multiplied with 10f results in an unscaled long value.
Conversely, a Decimal
value d
can be computed from the unscaled value u
as
d = u*10-f.
f
applied to unscaled decimal values within this DecimalArithmetic
objectScaleMetrics.getScale()
ScaleMetrics getScaleMetrics()
RoundingMode getRoundingMode()
DecimalArithmetic
object if rounding is
necessary.DecimalArithmetic
object if rounding is necessaryOverflowMode getOverflowMode()
DecimalArithmetic
object if an overflow
occurs. The overflow mode defines whether an operation should throw an exception if an overflow occurs.DecimalArithmetic
object if an overflow occursTruncationPolicy getTruncationPolicy()
TruncationPolicy
is defined by the overflow mode
and the
rounding mode
.DecimalArithmetic
handles truncationDecimalArithmetic deriveArithmetic(int scale)
scale
using this arithmetic's rounding mode
and overflow mode
.scale
- the scale for the new arithmetic; must be in [0,18]
both ends inclusiveIllegalArgumentException
- if scale is not in [0, 18]
Scales.getScaleMetrics(int)
,
ScaleMetrics.getArithmetic(RoundingMode)
,
ScaleMetrics.getArithmetic(TruncationPolicy)
DecimalArithmetic deriveArithmetic(RoundingMode roundingMode)
scale
as this arithmetic but for the specified
roundingMode
. The returned arithmetic uses the same overflow mode
as this
arithmetic.roundingMode
- the rounding mode for the new arithmeticNullPointerException
- if rounding mode is nullDecimalArithmetic deriveArithmetic(RoundingMode roundingMode, OverflowMode overflowMode)
scale
as this arithmetic but for the specified
roundingMode
and overflowMode
.roundingMode
- the rounding mode for the new arithmeticoverflowMode
- the overflow mode for the new arithmeticNullPointerException
- if any of the arguments is nullDecimalArithmetic deriveArithmetic(OverflowMode overflowMode)
scale
as this arithmetic but for the specified
overflowMode
. The returned arithmetic uses the same rounding mode
as this
arithmetic.overflowMode
- the overflow mode for the new arithmeticNullPointerException
- if overflow mode is nullDecimalArithmetic deriveArithmetic(TruncationPolicy truncationPolicy)
scale
as this arithmetic but with rounding and
overflow mode specified by the given truncationPolicy
.truncationPolicy
- the truncation policy specifying rounding and overflow mode for the new arithmeticNullPointerException
- if truncation policy is nulllong one()
1
. One is the value 10scale which
is also the multiplier used to get the unscaled decimal from the true decimal value.int signum(long uDecimal)
uDecimal
- the unscaled decimalint compare(long uDecimal1, long uDecimal2)
uDecimal1
- the first unscaled decimal to compareuDecimal2
- the second unscaled decimal to compare0
if unscaled1 == unscaled2
; a value less than 0
if
unscaled1 < unscaled2
; and a value greater than 0
if unscaled1 > unscaled2
int compareToUnscaled(long uDecimal, long unscaled, int scale)
scale
whereas the scale of the second unscaled
value is explicitly
specified by the scale
argument.uDecimal
- the first unscaled decimal to compareunscaled
- the second unscaled decimal to comparescale
- the scale of unscaled
0
if unscaled1 == unscaled2
; a value less than 0
if
unscaled1 < unscaled2
; and a value greater than 0
if unscaled1 > unscaled2
long add(long uDecimal1, long uDecimal2)
(uDecimal1 + uDecimal2)
.uDecimal1
- first unscaled decimal value to be addeduDecimal2
- second unscaled decimal value to be addeduDecimal1 + uDecimal2
ArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong addLong(long uDecimal, long lValue)
(uDecimal + lValue)
.
Mathematically the method calculates (uDecimal + lValue * 10scale) avoiding information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to be addedlValue
- long value to be addedArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong addUnscaled(long uDecimal, long unscaled, int scale)
rounding mode
is applied. Note that scale of the first
operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round(uDecimal + lValue * 10-scale + s) where s
refers to this arithetic's scale. The method avoids information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to be added tounscaled
- unscaled value to addscale
- scale associated with the unscaled
valueArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong subtract(long uDecimalMinuend, long uDecimalSubtrahend)
(uDecimalMinuend -
uDecimalSubtrahend)
.uDecimalMinuend
- unscaled decimal value to subtract fromuDecimalSubtrahend
- unscaled decimal value to subtract from the minuenduDecimalMinuend - uDecimalSubtrahend
ArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong subtractLong(long uDecimal, long lValue)
(uDecimal - lValue)
.
Mathematically the method calculates (uDecimal - lValue * 10scale) avoiding information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to subtract fromlValue
- long value to subtractArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong subtractUnscaled(long uDecimal, long unscaled, int scale)
rounding mode
is applied. Note that scale of the first
operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round(uDecimal - lValue * 10-scale + s) where s
refers to this arithetic's scale. The method avoids information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to subtract fromunscaled
- unscaled value to subtractscale
- scale associated with the unscaled
valueArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong multiply(long uDecimal1, long uDecimal2)
(uDecimal1 * uDecimal2)
. If rounding must be performed, this arithmetic's rounding mode
is applied.
Mathematically the method calculates round((uDecimal1 * uDecimal2) * 10-scale) avoiding information loss due to overflow of intermediary results.
uDecimal1
- first unscaled decimal value to be multiplieduDecimal2
- second unscaled decimal value to be multipliedround(uDecimal1 * uDecimal2)
ArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong multiplyByLong(long uDecimal, long lValue)
(uDecimal * lValue)
where the second argument is a true long
value instead of an unscaled decimal.uDecimal
- unscaled decimal value to be multipliedlValue
- long value to be multiplieduDecimal * lValue
ArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong multiplyByUnscaled(long uDecimal, long unscaled, int scale)
rounding mode
is applied. Note that scale of the first
operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round((uDecimal * unscaled) * 10-scale) avoiding information loss due to overflow of intermediary results.
uDecimal
- unscaled decimal value to be multipliedunscaled
- unscaled value to be multipliedscale
- scale associated with the unscaled
valueArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong multiplyByPowerOf10(long uDecimal, int n)
The power, n
, may be negative, in which case this method performs a division by a power of ten. If
rounding must be performed (for negative n), this arithmetic's rounding mode
is
applied.
uDecimal
- value to be multipliedn
- the power of tenArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong divide(long uDecimalDividend, long uDecimalDivisor)
(uDecimalDividend / uDecimalDivisor)
. If rounding must be performed, this arithmetic's
rounding mode
is applied.
Mathematically the method calculates round((uDecimalDividend * 10scale) / uDecimalDivisor) avoiding information loss due to overflow of intermediary results.
uDecimalDividend
- value to be divided.uDecimalDivisor
- value by which the dividend is to be divided.round(uDecimalDividend / uDecimalDivisor)
ArithmeticException
- if uDecimalDividend
is zero, if rounding mode
is UNNECESSARY and
rounding is necessary or if an overflow occurs and the overflow mode
is
set to throw an exceptionlong divideByLong(long uDecimalDividend, long lDivisor)
(uDecimalDividend / lDivisor)
where the second argument is a
true long value instead of an unscaled decimal. If rounding must be performed, this arithmetic's
rounding mode
is applied.uDecimalDividend
- value to be divided.lDivisor
- long value by which the dividend is to be divided.round(uDecimalDividend / lDivisor)
ArithmeticException
- if uDecimalDividend
is zero, if rounding mode
is UNNECESSARY and
rounding is necessary or if an overflow occurs and the overflow mode
is
set to throw an exceptionlong divideByUnscaled(long uDecimal, long unscaled, int scale)
rounding mode
is applied. Note that scale of the
first operand is determined by this arithmetic's scale
whereas the scale of the second
unscaled
value is explicitly specified by the scale
argument.
Mathematically the method calculates round((uDecimal * 10scale) / unscaled) avoiding information loss due to overflow of intermediary results.
uDecimal
- value to be divided.unscaled
- unscaled value by which the dividend is to be divided.scale
- scale associated with the unscaled
valueArithmeticException
- if uDecimal
is zero, if rounding mode
is UNNECESSARY and rounding
is necessary or if an overflow occurs and the overflow mode
is set to
throw an exceptionlong divideByPowerOf10(long uDecimal, int n)
rounding mode
is applied.
The power, n
, may be negative, in which case this method performs a multiplication by a power of ten.
uDecimal
- value to be divided.n
- the power of tenArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong avg(long uDecimal1, long uDecimal2)
uDecimal1
and uDecimal2
. The method is
much more efficient than an addition and subsequent long division and is guaranteed not to overflow. If rounding
must be performed, this arithmetic's rounding mode
is applied.uDecimal1
- the first unscaled decimal value to averageuDecimal2
- the second unscaled decimal value to averageround((uDecimal1 + uDecimal2) / 2)
ArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong abs(long uDecimal)
abs(uDecimal)
, which is the value itself if
uDecimal>=0
and -uDecimal
if the given value is negative.uDecimal
- the unscaled decimal valueabs(uDecimal)
ArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong negate(long uDecimal)
-uDecimal
.uDecimal
- the unscaled decimal value to negate-uDecimal
ArithmeticException
- if an overflow occurs and the overflow mode
is set to throw an exceptionlong invert(long uDecimal)
1/uDecimal
. If rounding must be
performed, this arithmetic's rounding mode
is applied.
Mathematically the method calculates round((10scale * 10scale) / uDecimalDivisor) avoiding information loss due to overflow of intermediary results.
uDecimal
- the unscaled decimal value to invertround(1/uDecimal)
ArithmeticException
- if uDecimal
is zero, if rounding mode
is UNNECESSARY and rounding
is necessary or if an overflow occurs and the overflow mode
is set to
throw an exceptionlong square(long uDecimal)
rounding mode
is applied.
Mathematically the method calculates round((uDecimal * uDecimal) * 10-scale) avoiding information loss due to overflow of intermediary results.
uDecimal
- the unscaled decimal value to be squaredround(uDecimal * uDecimal)
ArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong sqrt(long uDecimal)
rounding mode
is applied.
Mathematically the method calculates round(sqrt(uDecimal * 10scale)) avoiding information loss due to overflow of intermediary results.
uDecimal
- the unscaled decimal valueround(sqrt(uDecimal))
ArithmeticException
- if uDecimal
is negative or if rounding mode
is UNNECESSARY and
rounding is necessarylong pow(long uDecimalBase, int exponent)
exponent
is an integer rather than a decimal. If rounding must be performed, this arithmetic's rounding mode
is applied.
The current implementation uses the core algorithm defined in ANSI standard X3.274-1996. For exponent >= 0
, the
returned numerical value is within 1 ULP of the exact numerical value; the result is actually exact for all
rounding modes other than HALF_UP, HALF_EVEN and HALF_DOWN. No precision is guaranteed for exponent < 0
but the
result is usually exact up to 10-20 ULP.
Properties of the X3.274-1996 algorithm are:
IllegalArgumentException
is thrown if abs(n) > 999999999
exponent
is zero, one is returned even if uDecimalBase
is zero, otherwise
exponent
is positive, the result is calculated via the repeated squaring technique into a single
accumulatorexponent
is negative, the result is calculated as if exponent
were positive; this value is then divided
into onerounding mode
Note: this operation is not strictly garbage free since internally, two ThreadLocal
objects are
used to calculate the result. The ThreadLocal
values may become garbage if the thread becomes garbage.
uDecimalBase
- the unscaled decimal base valueexponent
- exponent to which uDecimalBase
is to be raised.IllegalArgumentException
- if abs(exponent) > 999999999
ArithmeticException
- if uDecimalBase==0
and exponent
is negative. (This would cause a division by zero.),
if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong shiftLeft(long uDecimal, int n)
(uDecimal << n)
. The shift distance, n
, may be
negative, in which case this method performs a right shift. The result is equal to
round(uDecimal * 2n) using this arithmetic's rounding mode
if
rounding is necessary.uDecimal
- the unscaled decimal value to shiftn
- shift distance, in bits.round(uDecimal << n)
ArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionshiftRight(long, int)
long shiftRight(long uDecimal, int n)
(uDecimal >> n)
. The shift distance, n
, may be
negative, in which case this method performs a left shift. The result is equal to
round(uDecimal / 2n) using this arithmetic's rounding mode
if
rounding is necessary.uDecimal
- the unscaled decimal value to shiftn
- shift distance, in bits.round(uDecimal >> n)
ArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionshiftLeft(long, int)
long round(long uDecimal, int precision)
precision
using the
rounding mode
of this arithmetic.
Note that this method does not change the scale of the value --- extra digits are simply zeroised.
Examples and special cases:
IllegalArgumentException
is thrownuDecimal
- the unscaled decimal value to roundprecision
- the precision to use for the rounding, for instance 2 to round to the second digit after the decimal
point; must be at least (scale - 18)
IllegalArgumentException
- if precision < scale - 18
ArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessary or if an
overflow occurs and the overflow mode
is set to throw an exceptionlong fromLong(long value)
scale
.value
- the value to convertIllegalArgumentException
- if value
is too large to be represented as a Decimal with the scale of this arithmeticlong fromFloat(float value)
scale
.value
- the value to convertIllegalArgumentException
- if value
is NaN or infinite or if the magnitude is too large for the float to be represented
as a Decimal
with the scale of this arithmeticArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong fromDouble(double value)
scale
.value
- the value to convertIllegalArgumentException
- if value
is NaN or infinite or if the magnitude is too large for the double to be represented
as a Decimal
with the scale of this arithmeticArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong fromBigInteger(BigInteger value)
BigInteger
value to an unscaled decimal. An exception is thrown if the specified
value is too large to be represented as a Decimal of this arithmetic's scale
.value
- the value to convertIllegalArgumentException
- if value
is too large to be represented as a Decimal with the scale of this arithmeticlong fromBigDecimal(BigDecimal value)
BigDecimal
value to an unscaled decimal. An exception is thrown if the specified
value is too large to be represented as a Decimal of this arithmetic's scale
.
Note: this operation is not garbage free, meaning that new temporary objects may be allocated during the conversion.
value
- the value to convertIllegalArgumentException
- if value
is too large to be represented as a Decimal with the scale of this arithmeticArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong fromUnscaled(long unscaledValue, int scale)
unscaledValue
- the unscaled decimal value to convertscale
- the scale associated with unscaledValue
IllegalArgumentException
- if the unscaled value with the specified scale is too large to be represented as a Decimal with the
scale of this arithmeticArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong parse(String value)
Decimal
into an unscaled Decimal. The string representation
consists of an optional sign, '+'
or '-'
, followed by a sequence of zero or more decimal digits
("the integer"), optionally followed by a fraction.
The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at
least one digit in either the integer or the fraction. If the fraction contains more digits than this
arithmetic's scale
, the value is rounded using the arithmetic's rounding mode
. An exception is thrown if the value is too large to be represented as a Decimal of this
arithmetic's scale.
value
- a String
containing the decimal value representation to be parsedlong
valueNumberFormatException
- if value
does not represent a valid Decimal
or if the value is too large to be
represented as a Decimal with the scale of this arithmeticArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong parse(CharSequence value, int start, int end)
Decimal
into an unscaled Decimal. The string representation
consists of an optional sign, '+'
or '-'
, followed by a sequence of zero or more decimal digits
("the integer"), optionally followed by a fraction.
The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at
least one digit in either the integer or the fraction. If the fraction contains more digits than this
arithmetic's scale
, the value is rounded using the arithmetic's rounding mode
. An exception is thrown if the value is too large to be represented as a Decimal of this
arithmetic's scale.
value
- a character sequence such as a String
containing the decimal value representation to be parsedstart
- the start index to read characters in value
, inclusiveend
- the end index where to stop reading in characters in value
, exclusivelong
valueIndexOutOfBoundsException
- if start < 0
or end > value.length()
NumberFormatException
- if value
does not represent a valid Decimal
or if the value is too large to be
represented as a Decimal with the scale of this arithmeticArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong toLong(long uDecimal)
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into a long valueuDecimal
value converted into a long value, possibly rounded or truncatedArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarylong toUnscaled(long uDecimal, int scale)
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into an unscaled value of the specified scalescale
- the target scale for the result valueuDecimal
value converted into an unscaled value of the specified scale, possibly rounded or
truncatedIllegalArgumentException
- if the unscaled value with this arithmetic's scale is too large to be represented as an unscaled
decimal with the specified scaleArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessaryfloat toFloat(long uDecimal)
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into a float valueuDecimal
value converted into a float value, possibly rounded or truncatedArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessarydouble toDouble(long uDecimal)
rounding mode
is applied if rounding is necessary.uDecimal
- the unscaled decimal value to convert into a double valueuDecimal
value converted into a double value, possibly rounded or truncatedArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessaryBigDecimal toBigDecimal(long uDecimal)
BigDecimal
value using this arithmetic's
scale
for the result value.
Note: this operation is not strictly garbage free since the result value is usually allocated; however no temporary objects other than the result are allocated during the conversion.
uDecimal
- the unscaled decimal value to convert into a BigDecimal
valueuDecimal
value converted into a BigDecimal
valueBigDecimal toBigDecimal(long uDecimal, int scale)
BigDecimal
value using the specified scale
for the result value. The arithmetic's rounding mode
is applied if rounding is
necessary.
Note: this operation is not garbage free since the result value is usually allocated and also temporary objects may be allocated during the conversion. Note however that temporary objects are only allocated if the unscaled value of the result exceeds the range of a long value.
uDecimal
- the unscaled decimal value to convert into a BigDecimal
valuescale
- the scale to use for the resulting BigDecimal
valueuDecimal
value converted into a BigDecimal
value, possibly rounded or truncatedArithmeticException
- if rounding mode
is UNNECESSARY and rounding is necessaryString toString(long uDecimal)
String
and returns it. If the scale
is zero, the conversion is identical to Long.toString(long)
. For all other scales a value with
exactly scale
fraction digits is returned even if some trailing fraction digits are zero.
Note: this operation is not strictly garbage free since the result value is allocated; however no
temporary objects other than the result are allocated during the conversion (internally a ThreadLocal
StringBuilder
object is used to construct the string value, which may become garbage if the thread
becomes garbage).
uDecimal
- the unscaled decimal value to convert into a String
uDecimal
value as into a String
void toString(long uDecimal, Appendable appendable) throws IOException
String
and appends the string to the
appendable
.
If the scale
is zero, the conversion into a string is identical to
Long.toString(long)
. For all other scales a string value with exactly scale
fraction digits is
created even if some trailing fraction digits are zero.
Note: this operation is not strictly garbage free since internally, a ThreadLocal
string builder
is used to construct the string. The ThreadLocal
value may become garbage if the thread becomes garbage.
uDecimal
- the unscaled decimal value to convert into a String
appendable
- the appendable to which the string representation of the unscaled decimal value is to be appendedIOException
- If an I/O error occurs when appending to appendable