|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Number
java.math.BigDecimal
public class BigDecimal
Field Summary | |
---|---|
static BigDecimal |
ONE
The constant one as a BigDecimal with scale zero. |
static int |
ROUND_CEILING
|
static int |
ROUND_DOWN
|
static int |
ROUND_FLOOR
|
static int |
ROUND_HALF_DOWN
|
static int |
ROUND_HALF_EVEN
|
static int |
ROUND_HALF_UP
|
static int |
ROUND_UNNECESSARY
|
static int |
ROUND_UP
|
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 Summary | |
---|---|
BigDecimal(BigInteger num)
Constructs a BigDecimal whose unscaled value is num and whose scale is zero. |
|
BigDecimal(BigInteger num,
int scale)
Constructs a BigDecimal whose unscaled value is num and whose scale is scale. |
|
BigDecimal(BigInteger num,
int scale,
MathContext mc)
Constructs a BigDecimal using the BigDecimal(BigInteger, int) constructor and then rounds according to the MathContext. |
|
BigDecimal(BigInteger num,
MathContext mc)
Constructs a BigDecimal whose value is given by num rounded according to mc. |
|
BigDecimal(char[] in)
Constructs a BigDecimal from the given char array, accepting the same sequence of characters as the BigDecimal(String) constructor. |
|
BigDecimal(char[] in,
int offset,
int len)
Constructs a BigDecimal from a char subarray, accepting the same sequence of characters as the BigDecimal(String) constructor. |
|
BigDecimal(char[] in,
int offset,
int len,
MathContext mc)
Constructs a BigDecimal from the char subarray and rounding according to the MathContext. |
|
BigDecimal(char[] in,
MathContext mc)
Constructs a BigDecimal from the char array and rounding according to the MathContext. |
|
BigDecimal(double num)
|
|
BigDecimal(double num,
MathContext mc)
Constructs a BigDecimal in the same way as BigDecimal(double) and then rounds according to the MathContext. |
|
BigDecimal(int val)
Constructs a new BigDecimal whose unscaled value is val and whose scale is zero. |
|
BigDecimal(int val,
MathContext mc)
Constructs a BigDecimal using the BigDecimal(int) constructor and then rounds according to the MathContext. |
|
BigDecimal(long val)
Constructs a new BigDecimal whose unscaled value is val and whose scale is zero. |
|
BigDecimal(long val,
MathContext mc)
Constructs a BigDecimal from the long in the same way as BigDecimal(long) and then rounds according to the MathContext. |
|
BigDecimal(String num)
|
|
BigDecimal(String val,
MathContext mc)
Constructs a BigDecimal from the String val according to the same rules as the BigDecimal(String) constructor and then rounds according to the MathContext mc. |
Method Summary | |
---|---|
BigDecimal |
abs()
|
BigDecimal |
abs(MathContext mc)
Returns a BigDecimal whose value is the absolute value of this BigDecimal with rounding according to the given MathContext. |
BigDecimal |
add(BigDecimal val)
|
BigDecimal |
add(BigDecimal val,
MathContext mc)
Returns a BigDecimal whose value is found first by calling the method add(val) and then by rounding according to the MathContext mc. |
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)
Compares this object with another, and returns a numerical result based on the comparison. |
BigDecimal |
divide(BigDecimal divisor)
Performs division, if the resulting quotient requires rounding (has a nonterminating decimal expansion), an ArithmeticException is thrown. |
BigDecimal |
divide(BigDecimal val,
int roundingMode)
|
BigDecimal |
divide(BigDecimal val,
int newScale,
int roundingMode)
|
BigDecimal |
divide(BigDecimal val,
int scale,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this / val), with the specified scale and rounding according to the RoundingMode |
BigDecimal |
divide(BigDecimal val,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this / val) rounded according to the RoundingMode |
BigDecimal[] |
divideAndRemainder(BigDecimal val)
Returns a BigDecimal array, the first element of which is the integer part of this / val, and the second element of which is the remainder of that quotient. |
BigDecimal |
divideToIntegralValue(BigDecimal val)
Returns a BigDecimal whose value is the integer part of the quotient this / val. |
double |
doubleValue()
Return the value of this Number as a float . |
boolean |
equals(Object o)
Determine whether this Object is semantically equal to another Object. |
float |
floatValue()
Return the value of this Number as a float . |
int |
hashCode()
Get a value that represents this Object, as uniquely as possible within the confines of an int. |
int |
intValue()
Return the value of this Number as an int . |
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()
Return the value of this Number as a long . |
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)
|
BigDecimal |
multiply(BigDecimal val,
MathContext mc)
Returns a BigDecimal whose value is (this x val) before it is rounded according to the MathContext mc. |
BigDecimal |
negate()
|
BigDecimal |
negate(MathContext mc)
Returns a BigDecimal whose value is found first by negating this via the negate() method, then by rounding according to the MathContext mc. |
BigDecimal |
plus()
Returns this BigDecimal. |
BigDecimal |
plus(MathContext mc)
Returns a BigDecimal whose value is found by rounding this
according to the MathContext. |
BigDecimal |
pow(int n)
Returns a BigDecimal whose value is this to the power of
n . |
BigDecimal |
pow(int n,
MathContext mc)
Returns a BigDecimal whose value is determined by first calling pow(n) and then by rounding according to the MathContext mc. |
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. |
BigDecimal |
round(MathContext mc)
Returns a BigDecimal which is this BigDecimal rounded according to the MathContext rounding settings. |
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)
|
BigDecimal |
setScale(int newScale,
RoundingMode roundingMode)
Returns a BigDecimal whose value is the same as this BigDecimal but whose representation has a scale of newScale . |
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)
|
BigDecimal |
subtract(BigDecimal val,
MathContext mc)
Returns a BigDecimal whose value is found first by calling the method subtract(val) and then by rounding according to the MathContext mc. |
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. |
String |
toEngineeringString()
Returns the String representation of this BigDecimal, using engineering notation if necessary. |
String |
toPlainString()
Returns a String representation of this BigDecimal without using scientific notation. |
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)
|
static BigDecimal |
valueOf(long val,
int scale)
|
Methods inherited from class java.lang.Number |
---|
byteValue, shortValue |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final BigDecimal ZERO
public static final BigDecimal ONE
public static final BigDecimal TEN
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
Constructor Detail |
---|
public BigDecimal(int val)
val
- the value of the new BigDecimalpublic BigDecimal(int val, MathContext mc)
val
- the value for the initial (unrounded) BigDecimalmc
- the MathContext specifying the rounding
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARYpublic BigDecimal(long val)
val
- the value of the new BigDecimalpublic BigDecimal(long val, MathContext mc)
val
- the long from which we create the initial BigDecimalmc
- the MathContext that specifies the rounding behaviour
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARYpublic BigDecimal(BigInteger num, MathContext mc)
num
- the unscaledValue, before roundingmc
- the MathContext that specifies the precision
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARY
* @since 1.5public BigDecimal(String val, MathContext mc)
val
- the String from which we construct the initial BigDecimalmc
- the MathContext that specifies the rounding
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARYpublic BigDecimal(BigInteger num)
num
- the value of the new BigDecimalpublic BigDecimal(BigInteger num, int scale)
num
- scale
- public BigDecimal(BigInteger num, int scale, MathContext mc)
num
- the unscaled value of the unrounded BigDecimalscale
- the scale of the unrounded BigDecimalmc
- the MathContext specifying the rounding
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARYpublic BigDecimal(double num, MathContext mc)
num
- the double from which the initial BigDecimal is createdmc
- the MathContext that specifies the rounding behaviour
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARYpublic BigDecimal(double num) throws NumberFormatException
NumberFormatException
public BigDecimal(char[] in, int offset, int len, MathContext mc)
in
- the char arrayoffset
- the start of the subarraylen
- the length of the subarraymc
- the MathContext for rounding
NumberFormatException
- if the char subarray is not a valid
BigDecimal representation
ArithmeticException
- if the result is inexact but the rounding
mode is RoundingMode.UNNECESSARYpublic BigDecimal(char[] in, MathContext mc)
in
- the char arraymc
- the MathContext
NumberFormatException
- if in
is not a valid BigDecimal
representation
ArithmeticException
- if the result is inexact but the rounding mode
is RoundingMode.UNNECESSARYpublic BigDecimal(char[] in)
in
- the char array
NumberFormatException
- if in
is not a valid BigDecimal
representationpublic BigDecimal(char[] in, int offset, int len)
in
- the char arrayoffset
- the start of the subarraylen
- the length of the subarray
NumberFormatException
- if in
is not a valid
BigDecimal representation.public BigDecimal(String num) throws NumberFormatException
NumberFormatException
Method Detail |
---|
public static BigDecimal valueOf(long val)
public static BigDecimal valueOf(long val, int scale) throws NumberFormatException
NumberFormatException
public BigDecimal add(BigDecimal val)
public BigDecimal add(BigDecimal val, MathContext mc)
val
- the augendmc
- the MathContext for rounding
this
+ val
, rounded if need be
ArithmeticException
- if the value is inexact but the rounding is
RoundingMode.UNNECESSARYpublic BigDecimal subtract(BigDecimal val)
public BigDecimal subtract(BigDecimal val, MathContext mc)
val
- the subtrahendmc
- the MathContext for rounding
this
- val
, rounded if need be
ArithmeticException
- if the value is inexact but the rounding is
RoundingMode.UNNECESSARYpublic BigDecimal multiply(BigDecimal val)
public BigDecimal multiply(BigDecimal val, MathContext mc)
val
- the multiplicandmc
- the MathContext for rounding
ArithmeticException
- if the value is inexact but the rounding mode
is RoundingMode.UNNECESSARYpublic BigDecimal divide(BigDecimal val, int roundingMode) throws ArithmeticException, IllegalArgumentException
ArithmeticException
IllegalArgumentException
public BigDecimal divide(BigDecimal val, int scale, RoundingMode roundingMode)
val
- the divisorscale
- the scale of the BigDecimal returnedroundingMode
- the rounding mode to use
ArithmeticException
- if divisor is zero or the rounding mode is
UNNECESSARY but the specified scale cannot represent the value exactlypublic BigDecimal divide(BigDecimal val, RoundingMode roundingMode)
val
- the divisorroundingMode
- the rounding mode to use
ArithmeticException
- if divisor is zero or the rounding mode is
UNNECESSARY but the specified scale cannot represent the value exactlypublic BigDecimal divide(BigDecimal val, int newScale, int roundingMode) throws ArithmeticException, IllegalArgumentException
ArithmeticException
IllegalArgumentException
public BigDecimal divide(BigDecimal divisor) throws ArithmeticException, IllegalArgumentException
ArithmeticException
IllegalArgumentException
public BigDecimal remainder(BigDecimal val)
val
- the divisor
ArithmeticException
- if val == 0public BigDecimal[] divideAndRemainder(BigDecimal val)
val
- the divisor
ArithmeticException
- if val == 0public BigDecimal divideToIntegralValue(BigDecimal val)
val
- the divisor
ArithmeticException
- if val == 0public int compareTo(BigDecimal val)
Comparable
o1.compareTo(o2) <op> 0
, where op
is one of <, <=, =, !=, >, or >=.
You must make sure that the comparison is mutual, ie.
sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
(where sgn() is
defined as -1, 0, or 1 based on the sign). This includes throwing an
exception in either direction if the two are not comparable; hence,
compareTo(null)
should always throw an Exception.
You should also ensure transitivity, in two forms:
x.compareTo(y) > 0 && y.compareTo(z) > 0
implies
x.compareTo(z) > 0
; and x.compareTo(y) == 0
implies x.compareTo(z) == y.compareTo(z)
.
compareTo
in interface Comparable<BigDecimal>
val
- the object to be compared
public boolean equals(Object o)
Object
There are some fairly strict requirements on this
method which subclasses must follow:
a.equals(b)
and
b.equals(c)
, then a.equals(c)
must be true as well.a.equals(b)
and
b.equals(a)
must have the same value.a.equals(a)
must
always be true.a.equals(null)
must be false.a.equals(b)
must imply
a.hashCode() == b.hashCode()
.
The reverse is not true; two objects that are not
equal may have the same hashcode, but that has
the potential to harm hashing performance.This is typically overridden to throw a ClassCastException
if the argument is not comparable to the class performing
the comparison, but that is not a requirement. It is legal
for a.equals(b)
to be true even though
a.getClass() != b.getClass()
. Also, it
is typical to never cause a NullPointerException
.
In general, the Collections API (java.util
) use the
equals
method rather than the ==
operator to compare objects. However, IdentityHashMap
is an exception to this rule, for its own good reasons.
The default implementation returns this == o
.
equals
in class Object
o
- the Object to compare to
Object.hashCode()
public int hashCode()
Object
There are some requirements on this method which
subclasses must follow:
a.equals(b)
is true, then
a.hashCode() == b.hashCode()
must be as well.
However, the reverse is not necessarily true, and two
objects may have the same hashcode without being equal.Notice that since hashCode
is used in
Hashtable
and other hashing classes,
a poor implementation will degrade the performance of hashing
(so don't blindly implement it as returning a constant!). Also,
if calculating the hash is time-consuming, a class may consider
caching the results.
The default implementation returns
System.identityHashCode(this)
hashCode
in class Object
Object.equals(Object)
,
System.identityHashCode(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 negate(MathContext mc)
mc
- the MathContext for rounding
ArithmeticException
- if the value is inexact but the rounding mode
is RoundingMode.UNNECESSARYpublic BigDecimal plus()
public BigDecimal plus(MathContext mc)
this
according to the MathContext. This is the same as round(MathContext).
mc
- the MathContext for rounding
this
before being rounded
ArithmeticException
- if the value is inexact but the rounding mode
is RoundingMode.UNNECESSARYpublic BigDecimal round(MathContext mc)
mc
- the MathContext that tells us how to round
public int precision()
public 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.
- Overrides:
toString
in class Object
- Returns:
- the String representing this Object, which may be null
- See Also:
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public String toEngineeringString()
public String toPlainString()
public BigInteger toBigInteger()
public BigInteger toBigIntegerExact()
public int intValue()
Number
Number
as an int
.
intValue
in class Number
public BigDecimal stripTrailingZeros()
public long longValue()
Number
Number
as a long
.
longValue
in class Number
public float floatValue()
Number
Number
as a float
.
floatValue
in class Number
public double doubleValue()
Number
Number
as a float
.
doubleValue
in class Number
public BigDecimal setScale(int scale) throws ArithmeticException
ArithmeticException
public BigDecimal setScale(int scale, int roundingMode) throws ArithmeticException, IllegalArgumentException
ArithmeticException
IllegalArgumentException
public BigDecimal setScale(int newScale, RoundingMode roundingMode)
newScale
. If the scale is
reduced then rounding may occur, according to the RoundingMode.
newScale
- roundingMode
-
this
with possible rounding
ArithmeticException
- if the rounding mode is UNNECESSARY but
rounding is requiredpublic static BigDecimal valueOf(double val)
val
- the double value used in Double.toString(double)
NumberFormatException
- if val is NaN or infinitepublic BigDecimal scaleByPowerOfTen(int n)
n
.
n
- the power of ten
public BigDecimal pow(int n)
this
to the power of
n
.
n
- the power
public BigDecimal pow(int n, MathContext mc)
n
- the powermc
- the MathContext
ArithmeticException
- if n < 0 or n > 999999999 or if the result is
inexact but the rounding is RoundingMode.UNNECESSARYpublic BigDecimal abs(MathContext mc)
mc
- the MathContext
public BigDecimal ulp()
this
.public long longValueExact()
ArithmeticException
- if rounding occurs or if overflow occurspublic int intValueExact()
long
returned from that
method fits into an int
.
this
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
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
ArithmeticException
- if this BigDecimal has a fractional part
or is too large to fit into a short.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |