public class BigDecimal extends java.lang.Object implements Comparable
BigDecimal
consists of an arbitrary precision integer unscaled value and a
32-bit integer scale. 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. The value of the number represented by the
BigDecimal
is therefore (unscaledValue × 10-scale).Modifier and Type | Field and Description |
---|---|
static BigDecimal |
ONE
The constant one as a
BigDecimal with scale zero. |
static int |
ROUND_CEILING
Rounding mode to round towards positive infinity.
|
static int |
ROUND_DOWN
Rounding mode to round towards zero.
|
static int |
ROUND_FLOOR
Rounding mode to round towards negative infinity.
|
static int |
ROUND_HALF_DOWN
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round down.
|
static int |
ROUND_HALF_EVEN
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round towards the even neighbor.
|
static int |
ROUND_HALF_UP
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round up.
|
static int |
ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
|
static int |
ROUND_UP
Rounding mode to round away from zero.
|
static BigDecimal |
TEN
The constant ten as a
BigDecimal with scale zero. |
static BigDecimal |
ZERO
The constant zero as a
BigDecimal with scale zero. |
Constructor and Description |
---|
BigDecimal(BigInteger num)
Constructs a new
BigDecimal whose unscaled value is num and whose scale is zero. |
BigDecimal(BigInteger num,
int scale)
Constructs a new
BigDecimal whose unscaled value is num and with the given scale. |
BigDecimal(double num)
Translates a
double into a BigDecimal which is the exact decimal representation of the double s binary
floating-point value. |
BigDecimal(int val)
Constructs a new
BigDecimal whose unscaled value is val and whose scale is zero. |
BigDecimal(long val)
Constructs a new
BigDecimal whose unscaled value is val and whose scale is zero. |
BigDecimal(java.lang.String num)
Translates the string representation of a
BigDecimal into a BigDecimal object. |
Modifier and Type | Method and Description |
---|---|
BigDecimal |
abs() |
BigDecimal |
add(BigDecimal val)
Returns a
BigDecimal whose value is (this + val) , and whose scale is max(this.scale(), val.scale()) . |
byte |
byteValueExact()
Converts this BigDecimal into a byte by first calling longValueExact and then checking that the
long
returned from that method fits into a byte . |
int |
compareTo(BigDecimal val) |
int |
compareTo(java.lang.Object other)
Must return > 0 if this object is greater than the other one, < 0 if its smaller, and 0 if they are equal.
|
BigDecimal |
divide(BigDecimal divisor)
Returns a
BigDecimal whose value is (this / val) , and whose scale is this.scale() . |
BigDecimal |
divide(BigDecimal val,
int roundingMode)
Returns a
BigDecimal whose value is (this / val) , and whose scale is this.scale() . |
BigDecimal |
divide(BigDecimal val,
int newScale,
int roundingMode)
Returns a
BigDecimal whose value is (this / val) , and whose scale is as specified. |
BigDecimal[] |
divideAndRemainder(BigDecimal val)
Returns a
BigDecimal array, where its first element is the integer part of this / val , and its second element is the
remainder of the division. |
BigDecimal |
divideToIntegralValue(BigDecimal val)
Returns a BigDecimal whose value is the integer part of the quotient this / val.
|
double |
doubleValue() |
boolean |
equals(java.lang.Object o) |
int |
hashCode() |
int |
intValue() |
int |
intValueExact()
Converts this BigDecimal into an int by first calling longValueExact and then checking that the
long
returned from that method fits into an int . |
long |
longValue() |
long |
longValueExact()
Converts this BigDecimal to a long value.
|
BigDecimal |
max(BigDecimal val) |
BigDecimal |
min(BigDecimal val) |
BigDecimal |
movePointLeft(int n) |
BigDecimal |
movePointRight(int n) |
BigDecimal |
multiply(BigDecimal val)
Returns a
BigDecimal whose value is (this × val), and whose scale is (this.scale() + val.scale()) . |
BigDecimal |
negate() |
BigDecimal |
plus()
Returns this BigDecimal.
|
BigDecimal |
pow(int n)
Returns a BigDecimal whose value is
this to the power of n . |
int |
precision()
Returns the precision of this BigDecimal (the number of digits in the unscaled value).
|
BigDecimal |
remainder(BigDecimal val)
Returns a
BigDecimal whose value is the remainder in the quotient this / val . |
int |
scale() |
BigDecimal |
scaleByPowerOfTen(int n)
Returns a BigDecimal whose numerical value is the numerical value of this BigDecimal multiplied by 10 to the power
of
n . |
BigDecimal |
setScale(int scale) |
BigDecimal |
setScale(int scale,
int roundingMode) |
short |
shortValueExact()
Converts this BigDecimal into a short by first calling longValueExact and then checking that the
long
returned from that method fits into a short . |
int |
signum() |
BigDecimal |
stripTrailingZeros()
Returns a BigDecimal which is numerically equal to this BigDecimal but with no trailing zeros in the
representation.
|
BigDecimal |
subtract(BigDecimal val)
Returns a
BigDecimal whose value is (this - val) , and whose scale is max(this.scale(), val.scale()) . |
BigInteger |
toBigInteger()
Converts this BigDecimal to a BigInteger.
|
BigInteger |
toBigIntegerExact()
Converts this BigDecimal into a BigInteger, throwing an ArithmeticException if the conversion is not exact.
|
java.lang.String |
toEngineeringString()
Returns the String representation of this BigDecimal, using engineering notation if necessary.
|
java.lang.String |
toPlainString()
Returns a String representation of this BigDecimal without using scientific notation.
|
java.lang.String |
toString()
Returns the String representation of this BigDecimal, using scientific notation if necessary.
|
BigDecimal |
ulp()
Returns the size of a unit in the last place of this BigDecimal.
|
BigInteger |
unscaledValue() |
static BigDecimal |
valueOf(double val)
Returns a new BigDecimal constructed from the BigDecimal(String) constructor using the Double .toString(double)
method to obtain the String.
|
static BigDecimal |
valueOf(long val)
Translates a
long value into a BigDecimal object with a scale of zero. |
static BigDecimal |
valueOf(long val,
int scale)
Translates a
long unscaled value and an int scale into a BigDecimal . |
public static final BigDecimal ZERO
BigDecimal
with scale zero.public static final BigDecimal ONE
BigDecimal
with scale zero.public static final BigDecimal TEN
BigDecimal
with scale zero.public static final int ROUND_UP
public static final int ROUND_DOWN
public static final int ROUND_CEILING
public static final int ROUND_FLOOR
public static final int ROUND_HALF_UP
public static final int ROUND_HALF_DOWN
public static final int ROUND_HALF_EVEN
public static final int ROUND_UNNECESSARY
public BigDecimal(java.lang.String num) throws InvalidNumberException
BigDecimal
into a BigDecimal
object.num
- The BigDecimal
as a string.InvalidNumberException
- If the given number is not a valid representation of a BigDecimal
.public BigDecimal(int val)
BigDecimal
whose unscaled value is val
and whose scale is zero.val
- The value of the new BigDecimal
.public BigDecimal(long val)
BigDecimal
whose unscaled value is val
and whose scale is zero.val
- The value of the new BigDecimal
.public BigDecimal(BigInteger num)
BigDecimal
whose unscaled value is num
and whose scale is zero.num
- The value of the new BigDecimal
.public BigDecimal(BigInteger num, int scale)
BigDecimal
whose unscaled value is num
and with the given scale.num
- The unscaled value of the new BigDecimal
.scale
- The given scale.public BigDecimal(double num) throws InvalidNumberException
double
into a BigDecimal
which is the exact decimal representation of the double
s binary
floating-point value. The scale of the returned BigDecimal
is the smallest value such that
(10scale × val) is an integer.num
- The number to be convertedInvalidNumberException
- If the number passed is infinite or NaN.public static BigDecimal valueOf(long val) throws InvalidNumberException
long
value into a BigDecimal
object with a scale of zero. This "static factory method"
is provided in preference to a (long)
constructor because it allows for reuse of frequently used BigDecimal
values.val
- The value of the BigDecimal
.BigDecimal
whose value is val
.InvalidNumberException
public static BigDecimal valueOf(long val, int scale) throws InvalidNumberException
long
unscaled value and an int
scale into a BigDecimal
. This "static factory method" is
provided in preference to a (long, int)
constructor because it allows for reuse of frequently used BigDecimal
values.scale
- Scale of the BigDecimal
.BigDecimal
whose value is (unscaledVal × 10-scale).InvalidNumberException
public BigDecimal add(BigDecimal val)
BigDecimal
whose value is (this + val)
, and whose scale is max(this.scale(), val.scale())
.val
- The value to be added to this BigDecimal
.(this + val)
.public BigDecimal subtract(BigDecimal val)
BigDecimal
whose value is (this - val)
, and whose scale is max(this.scale(), val.scale())
.val
- The value to be subtracted from this BigDecimal
.(this - val)
.public BigDecimal multiply(BigDecimal val)
BigDecimal
whose value is (this × val), and whose scale is (this.scale() + val.scale())
.val
- The value to be multiplied by this BigDecimal
.(this * val)
.public BigDecimal divide(BigDecimal val, int roundingMode) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
BigDecimal
whose value is (this / val)
, and whose scale is this.scale()
. If rounding must be
performed to generate a result with the given scale, the specified rounding mode is applied.val
- The value by which this BigDecimal
is to be divided.roundingMode
- The rounding mode to apply.(this / val)
.java.lang.ArithmeticException
- If val == 0
, or roundingMode == ROUND_UNNECESSARY
and this.scale()
is
insufficient to represent the result of the division exactly.java.lang.IllegalArgumentException
- If roundingMode
does not represent a valid rounding mode.public BigDecimal divide(BigDecimal val, int newScale, int roundingMode) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
BigDecimal
whose value is (this / val)
, and whose scale is as specified. If rounding must be
performed to generate a result with the given scale, the specified rounding mode is applied.val
- The value by which this BigDecimal
is to be divided.roundingMode
- The rounding mode to apply.(this / val)
.java.lang.ArithmeticException
- If val == 0
, or roundingMode == ROUND_UNNECESSARY
and this.scale()
is
insufficient to represent the result of the division exactly.java.lang.IllegalArgumentException
- If roundingMode
does not represent a valid rounding mode.public BigDecimal divide(BigDecimal divisor) throws java.lang.ArithmeticException
BigDecimal
whose value is (this / val)
, and whose scale is this.scale()
. If rounding must be
performed to generate a result with the given scale, an ArithmeticException
is thrown.divisor
- The value by which this BigDecimal
is to be divided.(this / val)
.java.lang.ArithmeticException
- If val == 0
, or this.scale()
is insufficient to represent the result of the division
exactly.public BigDecimal remainder(BigDecimal val) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException, InvalidNumberException
BigDecimal
whose value is the remainder in the quotient this / val
. This is obtained by
subtract(divideToIntegralValue(val).multiply(val))
.val
- The divisor.BigDecimal
whose value is the remainderInvalidNumberException
- If an internal method throws it.java.lang.IllegalArgumentException
- If an internal method throws it.java.lang.ArithmeticException
- If val == 0
.public BigDecimal[] divideAndRemainder(BigDecimal val) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException, InvalidNumberException
BigDecimal
array, where its first element is the integer part of this / val
, and its second element is the
remainder of the division.val
- The divisor.BigDecimal
array.InvalidNumberException
- If an internal method throws it.java.lang.IllegalArgumentException
- If an internal method throws it.java.lang.ArithmeticException
- If val == 0
.public BigDecimal divideToIntegralValue(BigDecimal val) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException, InvalidNumberException
val
- the divisorInvalidNumberException
java.lang.IllegalArgumentException
java.lang.ArithmeticException
java.lang.ArithmeticException
- if val == 0public int compareTo(BigDecimal val)
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public BigDecimal max(BigDecimal val)
public BigDecimal min(BigDecimal val)
public BigDecimal movePointLeft(int n)
public BigDecimal movePointRight(int n)
public int signum()
public int scale()
public BigInteger unscaledValue()
public BigDecimal abs()
public BigDecimal negate()
public BigDecimal plus()
public int precision()
public java.lang.String toString()
scale == 0
is returned.
2. an int adjExp
is created which is equal to the negation of scale
plus the number of
digits in the unscaled value, minus one. 3. if scale >= 0 && adjExp >= -6
then we represent this
BigDecimal without scientific notation. A decimal is added if the scale is positive and zeros are prepended as
necessary. 4. if scale is negative or adjExp is less than -6 we use scientific notation. If the unscaled value has
more than one digit, a decimal as inserted after the first digit, the character 'E' is appended and adjExp is
appended.toString
in class java.lang.Object
public java.lang.String toEngineeringString()
public java.lang.String toPlainString()
public BigInteger toBigInteger()
public BigInteger toBigIntegerExact()
public int intValue()
public BigDecimal stripTrailingZeros() throws InvalidNumberException
InvalidNumberException
public long longValue()
public double doubleValue() throws InvalidNumberException
InvalidNumberException
public BigDecimal setScale(int scale) throws java.lang.ArithmeticException
java.lang.ArithmeticException
public BigDecimal setScale(int scale, int roundingMode) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
java.lang.ArithmeticException
java.lang.IllegalArgumentException
public static BigDecimal valueOf(double val) throws InvalidNumberException
val
- the double value used in Double .toString(double)InvalidNumberException
- if val is NaN or infinitepublic BigDecimal scaleByPowerOfTen(int n)
n
.n
- the power of tenpublic BigDecimal pow(int n)
this
to the power of n
.n
- the powerpublic BigDecimal ulp()
this
.public long longValueExact()
java.lang.ArithmeticException
- if rounding occurs or if overflow occurspublic int intValueExact()
long
returned from that method fits into an int
.this
java.lang.ArithmeticException
- if this BigDecimal has a fractional part or is too large to fit into an int.public byte byteValueExact()
long
returned from that method fits into a byte
.this
java.lang.ArithmeticException
- if this BigDecimal has a fractional part or is too large to fit into a byte.public short shortValueExact()
long
returned from that method fits into a short
.this
java.lang.ArithmeticException
- if this BigDecimal has a fractional part or is too large to fit into a short.public int compareTo(java.lang.Object other) throws java.lang.ClassCastException
Comparable
compareTo
in interface Comparable
java.lang.ClassCastException