public final class Decimal extends Number implements Serializable, Comparable<Decimal>
DecimalType.MAX_SCALE
. This class has basically the same
functionality as BigDecimal
, but it represents equal numbers with
different notations in canonical form and thus they are always equal (e.g.
numbers 1.0 and 1.00 are equal).BigDecimal
,
Serialized FormModifier and Type | Field and Description |
---|---|
static Decimal |
MINUS_ONE
Decimal with value -1.
|
static Decimal |
ONE
Decimal with value 1.
|
static Decimal |
ZERO
Decimal with value 0.
|
Constructor and Description |
---|
Decimal(BigDecimal val)
Creates new instance of
Decimal . |
Decimal(BigInteger val)
Creates new instance of
Decimal . |
Decimal(char[] val)
Creates new instance of
Decimal . |
Decimal(double val)
Creates new instance of
Decimal . |
Decimal(long val)
Creates new instance of
Decimal . |
Decimal(String val)
Creates new instance of
Decimal . |
Modifier and Type | Method and Description |
---|---|
Decimal |
abs()
Returns a
Decimal whose value is the absolute value of this
Decimal , and whose scale is this.scale() . |
Decimal |
add(Decimal augend)
Returns a
Decimal whose value is (this + augend) . |
int |
compareTo(Decimal val)
Compares this
Decimal with the specified Decimal . |
Decimal |
divide(Decimal divisor)
Returns a
Decimal whose value is (this / divisor) . |
Decimal |
divideToIntegralValue(Decimal divisor)
Returns a
Decimal whose value is the integer part of the quotient
(this / divisor) rounded down. |
double |
doubleValue()
Converts this
Decimal to a double . |
boolean |
equals(Object obj)
Compares this
Decimal with the specified Object for
equality. |
float |
floatValue()
Converts this
Decimal to a float . |
int |
hashCode()
Returns the hash code for this
Decimal . |
int |
intValue()
Converts this
Decimal to an int . |
int |
intValueExact()
Converts this
Decimal to an int , checking for lost
information. |
boolean |
isInteger()
Returns true if this
Decimal is an integer, i.e. |
long |
longValue()
Converts this
Decimal to a long . |
long |
longValueExact()
Converts this
Decimal to a long , checking for lost
information. |
Decimal |
multiply(Decimal multiplicand)
Returns a
Decimal whose value is (this ×
multiplicand) . |
Decimal |
negate()
Returns a
Decimal whose value is (-this) . |
Decimal |
pow(int n)
Returns a
Decimal whose value is (this<sup>n</sup>) , The
power is computed exactly, to unlimited precision. |
int |
precision()
Returns the precision of this
Decimal . |
Decimal |
remainder(Decimal divisor)
Returns a
Decimal whose value is (this % divisor) . |
int |
scale()
Returns the scale of this
Decimal . |
Decimal |
setScale(int newScale,
RoundingMode roundingMode)
Returns a
Decimal whose scale is the specified value, and whose
unscaled value is determined by multiplying or dividing this Decimal 's unscaled value by the appropriate power of ten to maintain its
overall value. |
int |
signum()
Returns the signum function of this
Decimal . |
Decimal |
subtract(Decimal subtrahend)
Returns a
Decimal whose value is (this - subtrahend) . |
BigDecimal |
toBigDecimal()
Converts this
Decimal to a BigDecimal . |
BigInteger |
toBigInteger()
Converts this
Decimal to a BigInteger . |
BigInteger |
toBigIntegerExact()
Converts this
Decimal to a BigInteger , checking for lost
information. |
String |
toPlainString()
Returns a string representation of this
Decimal without an
exponent field. |
String |
toString()
This method is identical to
BigDecimal.toString() ; only in the
case when the scale is in the range [-1, -20], the scale is extended to 0
by adding zeros and only after then the BigDecimal.toString()
method is performed. |
BigInteger |
unscaledValue()
Returns a
BigInteger whose value is the unscaled value of
this Decimal . |
static Decimal |
valueOf(double val)
Translates a double into a Decimal, using the
double's canonical string representation provided by the
Double.toString(double) method. |
static Decimal |
valueOf(float val)
Translates a float into a Decimal, using the
float's canonical string representation provided by the
Float.toString(float) method. |
static Decimal |
valueOf(int val)
Translates a int value into a Decimal with a scale of
zero.
|
static Decimal |
valueOf(long val)
Translates a long value into a Decimal with a scale of
zero.
|
static Decimal |
valueOf(Number number)
Attempts to construct the decimal using a best-fit algorithm:
Long, Integer, Short, Byte uses the
valueOf(long) method
Float uses valueOf(float)
Double uses valueOf(double)
BigInteger uses Decimal(java.math.BigInteger)
BigDecimal uses Decimal(java.math.BigDecimal)
Decimal just return the number
All other numbers are rejected to avoid precision loss. |
byteValue, shortValue
public static final Decimal ZERO
public static final Decimal ONE
public static final Decimal MINUS_ONE
public Decimal(long val)
Decimal
.val
- public Decimal(double val)
Decimal
.val
- NumberFormatException
- if val
is infinite or NaNpublic Decimal(String val)
Decimal
.val
- NumberFormatException
- if val
is not a valid representation of a Decimal
NullPointerException
- if val
is nullpublic Decimal(char[] val)
Decimal
.val
- NumberFormatException
- if val
is not a valid representation of a Decimal
NullPointerException
- if val
is nullpublic Decimal(BigInteger val)
Decimal
.val
- NullPointerException
- if val
is nullpublic Decimal(BigDecimal val)
Decimal
.val
- public Decimal abs()
Decimal
whose value is the absolute value of this
Decimal
, and whose scale is this.scale()
.abs(this)
public Decimal add(Decimal augend)
Decimal
whose value is (this + augend)
.augend
- value to be added to this Decimal
.this + augend
NullPointerException
- if augend
is nullpublic Decimal subtract(Decimal subtrahend)
Decimal
whose value is (this - subtrahend)
.subtrahend
- value to be subtracted from this Decimal
.this - subtrahend
NullPointerException
- if subtrahend
is nullpublic Decimal multiply(Decimal multiplicand)
Decimal
whose value is (this ×
multiplicand)
.multiplicand
- value to be multiplied by this Decimal
.this * multiplicand
NullPointerException
- if multiplicand
is nullpublic Decimal divide(Decimal divisor)
Decimal
whose value is (this / divisor)
.divisor
- value by which this Decimal
is to be divided.this / divisor
ArithmeticException
- if divisor
is zeroNullPointerException
- if divisor
is nullpublic Decimal divideToIntegralValue(Decimal divisor)
Decimal
whose value is the integer part of the quotient
(this / divisor)
rounded down.divisor
- value by which this Decimal
is to be divided.this / divisor
.ArithmeticException
- if divisor==0
ArithmeticException
- if divisor
is zeroNullPointerException
- if divisor
is nullpublic Decimal negate()
Decimal
whose value is (-this)
.-this
.public Decimal remainder(Decimal divisor)
Decimal
whose value is (this % divisor)
.
The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))
.
Note that this is not the modulo operation (the result can be negative).
divisor
- value by which this Decimal
is to be divided.this % divisor
.ArithmeticException
- if divisor
is zeroNullPointerException
- if divisor
is nullpublic Decimal pow(int n)
Decimal
whose value is (this<sup>n</sup>)
, The
power is computed exactly, to unlimited precision.
The parameter n
must be in the range 0 through 999999999,
inclusive. ZERO.pow(0)
returns ONE
.
n
- power to raise this Decimal
to.this<sup>n</sup>
ArithmeticException
- if n
is out of range.public int signum()
Decimal
.Decimal
is negative,
zero, or positive.public int scale()
Decimal
. If zero or positive,
the scale is the number of digits to the right of the decimal point. If
negative, the unscaled value of the number is multiplied by ten to the
power of the negation of the scale. For example, a scale of -3
means the unscaled value is multiplied by 1000.Decimal
.public Decimal setScale(int newScale, RoundingMode roundingMode)
Decimal
whose scale is the specified value, and whose
unscaled value is determined by multiplying or dividing this Decimal
's unscaled value by the appropriate power of ten to maintain its
overall value. If the scale is reduced by the operation, the unscaled
value must be divided (rather than multiplied), and the value may be
changed; in this case, the specified rounding mode is applied to the
division.newScale
- scale of the Decimal
value to be returned.roundingMode
- The rounding mode to apply.Decimal
whose scale is the specified value, and whose
unscaled value is determined by multiplying or dividing this
Decimal
's unscaled value by the appropriate power of ten
to maintain its overall value.ArithmeticException
- if roundingMode==UNNECESSARY
and the specified
scaling operation would require rounding.NullPointerException
- if roundingMode
is nullRoundingMode
public int precision()
Decimal
. (The precision is
the number of digits in the unscaled value.)
The precision of a zero value is 1.
Decimal
.public BigInteger unscaledValue()
BigInteger
whose value is the unscaled value of
this Decimal
. (Computes (this *
10<sup>this.scale()</sup>)
.)Decimal
.public float floatValue()
Decimal
to a float
.floatValue
in class Number
Decimal
converted to a float
.BigDecimal.floatValue()
public double doubleValue()
Decimal
to a double
.doubleValue
in class Number
Decimal
converted to a double
.BigDecimal.doubleValue()
public int intValue()
Decimal
to an int
.intValue
in class Number
Decimal
converted to an int
.BigDecimal.intValue()
public int intValueExact()
Decimal
to an int
, checking for lost
information. If this Decimal
has a nonzero fractional part or is
out of the possible range for an int
result then an ArithmeticException
is thrown.Decimal
converted to an int
.ArithmeticException
- if this
has a nonzero fractional part, or will not
fit in an int
.public long longValue()
Decimal
to a long
.longValue
in class Number
Decimal
converted to a long
.BigDecimal.longValue()
public long longValueExact()
Decimal
to a long
, checking for lost
information. If this Decimal
has a nonzero fractional part or is
out of the possible range for a long
result then an ArithmeticException
is thrown.Decimal
converted to a long
.ArithmeticException
- if this
has a nonzero fractional part, or will not
fit in a long
.public BigDecimal toBigDecimal()
Decimal
to a BigDecimal
.Decimal
converted to a BigDecimal
.public BigInteger toBigInteger()
Decimal
to a BigInteger
.Decimal
> converted to a BigInteger
.BigDecimal.toBigInteger()
public BigInteger toBigIntegerExact()
Decimal
to a BigInteger
, checking for lost
information. An exception is thrown if this Decimal
has a nonzero
fractional part.Decimal
converted to a BigInteger
.ArithmeticException
- if this
has a nonzero fractional part.public boolean isInteger()
Decimal
is an integer, i.e. x.scale() <= 0
.Decimal
is an integerpublic int compareTo(Decimal val)
Decimal
with the specified Decimal
. This
method is provided in preference to individual methods for each of the
six boolean comparison operators (<, ==, >, >=, !=, <=). The
suggested idiom for performing these comparisons is: (x.compareTo(y)
<op> 0)
, where <op> is
one of the six comparison operators.compareTo
in interface Comparable<Decimal>
val
- Decimal
to which this Decimal
is to be
compared.Decimal
is numerically less than,
equal to, or greater than val
.NullPointerException
- if val
is nullpublic int hashCode()
Decimal
.hashCode
in class Object
Decimal
.equals(Object)
public boolean equals(Object obj)
Decimal
with the specified Object
for
equality. This method returns true if and only if the obj
is
Decimal
and this.compareTo(obj) == 0
holds (thus 2.0 is
equal to 2.00 when compared by this method).equals
in class Object
obj
- Object
to which this Decimal
is to be
compared.true
if and only if the specified Object
is a
Decimal
whose value and scale are equal to this Decimal
's.compareTo(com.whitestein.lsps.lang.Decimal)
,
hashCode()
public String toString()
BigDecimal.toString()
; only in the
case when the scale is in the range [-1, -20], the scale is extended to 0
by adding zeros and only after then the BigDecimal.toString()
method is performed.
The canonical representation of Decimal
means that decimals with
different notation, but with equal value will always have the same
toString representation.
toString
in class Object
BigDecimal.toString()
public String toPlainString()
Decimal
without an
exponent field.Decimal
without an
exponent field.BigDecimal.toPlainString()
public static Decimal valueOf(int val)
val
- value of the Decimal.public static Decimal valueOf(long val)
val
- value of the Decimal.public static Decimal valueOf(Number number)
valueOf(long)
methodvalueOf(float)
valueOf(double)
Decimal(java.math.BigInteger)
Decimal(java.math.BigDecimal)
number
- public static Decimal valueOf(double val)
Double.toString(double)
method.
Note: This is generally the preferred way to convert a
double into a Decimal, as the value
returned is equal to that resulting from constructing a Decimal
from the result of using Double.toString(double)
.
val
- double to convert to a Decimal.NumberFormatException
- if val is infinite or NaN.public static Decimal valueOf(float val)
Float.toString(float)
method.
Note: This is generally the preferred way to convert a
float into a Decimal, as the value
returned is equal to that resulting from constructing a Decimal
from the result of using Float.toString(float)
.
val
- float to convert to a Decimal.NumberFormatException
- if val is infinite or NaN.Copyright © 2007-2020 Whitestein Technologies. All Rights Reserved.