ONE
public static final BigDecimal ONE
The constant one as a BigDecimal with scale zero.
ROUND_UP
public static final int ROUND_UP
TEN
public static final BigDecimal TEN
The constant ten as a BigDecimal with scale zero.
ZERO
public static final BigDecimal ZERO
The constant zero as a BigDecimal with scale zero.
BigDecimal
public BigDecimal(char[] in)
Constructs a BigDecimal from the given char array, accepting the same
sequence of characters as the BigDecimal(String) constructor.
BigDecimal
public 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.
in
- the char arrayoffset
- the start of the subarraylen
- the length of the subarray
BigDecimal
public BigDecimal(char[] in,
int offset,
int len,
MathContext mc)
Constructs a BigDecimal from the char subarray and rounding
according to the MathContext.
in
- the char arrayoffset
- the start of the subarraylen
- the length of the subarraymc
- the MathContext for rounding
BigDecimal
public BigDecimal(char[] in,
MathContext mc)
Constructs a BigDecimal from the char array and rounding according
to the MathContext.
in
- the char arraymc
- the MathContext
BigDecimal
public BigDecimal(double num,
MathContext mc)
Constructs a BigDecimal in the same way as BigDecimal(double) and then
rounds according to the MathContext.
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.UNNECESSARY
BigDecimal
public BigDecimal(int val)
Constructs a new BigDecimal whose unscaled value is val and whose
scale is zero.
val
- the value of the new BigDecimal
BigDecimal
public BigDecimal(int val,
MathContext mc)
Constructs a BigDecimal using the BigDecimal(int) constructor and then
rounds according to the MathContext.
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.UNNECESSARY
BigDecimal
public 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.
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.UNNECESSARY
BigDecimal
public BigDecimal(BigInteger num)
Constructs a BigDecimal whose unscaled value is num and whose
scale is zero.
num
- the value of the new BigDecimal
BigDecimal
public BigDecimal(BigInteger num,
int scale)
Constructs a BigDecimal whose unscaled value is num and whose
scale is scale.
BigDecimal
public BigDecimal(BigInteger num,
int scale,
MathContext mc)
Constructs a BigDecimal using the BigDecimal(BigInteger, int)
constructor and then rounds according to the MathContext.
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.UNNECESSARY
BigDecimal
public BigDecimal(BigInteger num,
MathContext mc)
Constructs a BigDecimal whose value is given by num rounded according to
mc. Since num is already a BigInteger, the rounding refers only to the
precision setting in mc, if mc.getPrecision() returns an int lower than
the number of digits in num, then rounding is necessary.
num
- the unscaledValue, before roundingmc
- the MathContext that specifies the precision
ArithmeticException
- if the result is inexact but the rounding type
is RoundingMode.UNNECESSARY
BigDecimal
public BigDecimal(long val)
Constructs a new BigDecimal whose unscaled value is val and whose
scale is zero.
val
- the value of the new BigDecimal
BigDecimal
public 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.
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.UNNECESSARY
abs
public BigDecimal abs(MathContext mc)
Returns a BigDecimal whose value is the absolute value of this BigDecimal
with rounding according to the given MathContext.
add
public 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.
val
- the augendmc
- the MathContext for rounding
this
+ val
, rounded if need be
byteValueExact
public 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
.
- a byte whose value is
this
ArithmeticException
- if this BigDecimal has a fractional part
or is too large to fit into a byte.
divide
public 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
val
- the divisorscale
- the scale of the BigDecimal returnedroundingMode
- the rounding mode to use
- a BigDecimal whose value is approximately (this / val)
ArithmeticException
- if divisor is zero or the rounding mode is
UNNECESSARY but the specified scale cannot represent the value exactly
divide
public BigDecimal divide(BigDecimal val,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this / val) rounded according to the
RoundingMode
val
- the divisorroundingMode
- the rounding mode to use
- a BigDecimal whose value is approximately (this / val)
ArithmeticException
- if divisor is zero or the rounding mode is
UNNECESSARY but the specified scale cannot represent the value exactly
divideAndRemainder
public 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.
- the above described BigDecimal array
divideToIntegralValue
public BigDecimal divideToIntegralValue(BigDecimal val)
Returns a BigDecimal whose value is the integer part of the quotient
this / val. The preferred scale is this.scale - val.scale.
- a BigDecimal whose value is the integer part of this / val.
equals
public boolean equals(Object o)
Determine whether this Object is semantically equal
to another Object.
There are some fairly strict requirements on this
method which subclasses must follow:
- It must be transitive. If
a.equals(b)
and
b.equals(c)
, then a.equals(c)
must be true as well. - It must be symmetric.
a.equals(b)
and
b.equals(a)
must have the same value. - It must be reflexive.
a.equals(a)
must
always be true. - It must be consistent. Whichever value a.equals(b)
returns on the first invocation must be the value
returned on all later invocations.
a.equals(null)
must be false.- It must be consistent with hashCode(). That is,
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 interface Object
- whether this Object is semantically equal to another
hashCode
public int hashCode()
Get a value that represents this Object, as uniquely as
possible within the confines of an int.
There are some requirements on this method which
subclasses must follow:
- Semantic equality implies identical hashcodes. In other
words, if
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. - It must be consistent. Whichever value o.hashCode()
returns on the first invocation must be the value
returned on all later invocations as long as the object
exists. Notice, however, that the result of hashCode may
change between separate executions of a Virtual Machine,
because it is not invoked on the same object.
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 interface Object
- the hash code for this Object
intValueExact
public 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
.
- an int whose value is
this
ArithmeticException
- if this BigDecimal has a fractional part
or is too large to fit into an int.
longValueExact
public long longValueExact()
Converts this BigDecimal to a long value.
multiply
public BigDecimal multiply(BigDecimal val,
MathContext mc)
Returns a BigDecimal whose value is (this x val) before it is rounded
according to the MathContext mc.
val
- the multiplicandmc
- the MathContext for rounding
- a new BigDecimal with value approximately (this x val)
ArithmeticException
- if the value is inexact but the rounding mode
is RoundingMode.UNNECESSARY
negate
public 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.
mc
- the MathContext for rounding
- a BigDecimal whose value is approximately (-this)
ArithmeticException
- if the value is inexact but the rounding mode
is RoundingMode.UNNECESSARY
plus
public BigDecimal plus()
Returns this BigDecimal. This is included for symmetry with the
method negate().
plus
public BigDecimal plus(MathContext mc)
Returns a BigDecimal whose value is found by rounding this
according to the MathContext. This is the same as round(MathContext).
mc
- the MathContext for rounding
- a BigDecimal whose value is
this
before being rounded
ArithmeticException
- if the value is inexact but the rounding mode
is RoundingMode.UNNECESSARY
pow
public BigDecimal pow(int n)
Returns a BigDecimal whose value is this
to the power of
n
.
pow
public 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.
n
- the powermc
- the MathContext
ArithmeticException
- if n <320 or n > 999999999 or if the result is
inexact but the rounding is RoundingMode.UNNECESSARY
precision
public int precision()
Returns the precision of this BigDecimal (the number of digits in the
unscaled value). The precision of a zero value is 1.
- the number of digits in the unscaled value, or 1 if the value
is zero.
remainder
public BigDecimal remainder(BigDecimal val)
Returns a BigDecimal whose value is the remainder in the quotient
this / val. This is obtained by
subtract(divideToIntegralValue(val).multiply(val)).
- a BigDecimal whose value is the remainder
round
public BigDecimal round(MathContext mc)
Returns a BigDecimal which is this BigDecimal rounded according to the
MathContext rounding settings.
mc
- the MathContext that tells us how to round
scaleByPowerOfTen
public 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
.
setScale
public 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
. If the scale is
reduced then rounding may occur, according to the RoundingMode.
newScale
- roundingMode
-
- a BigDecimal whose scale is as given, whose value is
this
with possible rounding
shortValueExact
public 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
.
- a short whose value is
this
ArithmeticException
- if this BigDecimal has a fractional part
or is too large to fit into a short.
stripTrailingZeros
public BigDecimal stripTrailingZeros()
Returns a BigDecimal which is numerically equal to this BigDecimal but
with no trailing zeros in the representation. For example, if this
BigDecimal has [unscaledValue, scale] = [6313000, 4] this method returns
a BigDecimal with [unscaledValue, scale] = [6313, 1]. As another
example, [12400, -2] would become [124, -4].
- a numerically equal BigDecimal with no trailing zeros
subtract
public 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.
val
- the subtrahendmc
- the MathContext for rounding
this
- val
, rounded if need be
toBigInteger
public BigInteger toBigInteger()
Converts this BigDecimal to a BigInteger. Any fractional part will
be discarded.
- a BigDecimal whose value is equal to floor[this]
toBigIntegerExact
public BigInteger toBigIntegerExact()
Converts this BigDecimal into a BigInteger, throwing an
ArithmeticException if the conversion is not exact.
- a BigInteger whose value is equal to the value of this BigDecimal
toEngineeringString
public String toEngineeringString()
Returns the String representation of this BigDecimal, using engineering
notation if necessary. This is similar to toString() but when exponents
are used the exponent is made to be a multiple of 3 such that the integer
part is between 1 and 999.
- a String representation of this BigDecimal in engineering notation
toPlainString
public String toPlainString()
Returns a String representation of this BigDecimal without using
scientific notation. This is how toString() worked for releases 1.4
and previous. Zeros may be added to the end of the String. For
example, an unscaled value of 1234 and a scale of -3 would result in
the String 1234000, but the toString() method would return
1.234E+6.
- a String representation of this BigDecimal
toString
public String toString()
Returns the String representation of this BigDecimal, using scientific
notation if necessary. The following steps are taken to generate
the result:
1. the BigInteger unscaledValue's toString method is called and if
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 interface Object
ulp
public BigDecimal ulp()
Returns the size of a unit in the last place of this BigDecimal. This
returns a BigDecimal with [unscaledValue, scale] = [1, this.scale()].
- the size of a unit in the last place of
this
.
valueOf
public 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.
val
- the double value used in Double.toString(double)
- a BigDecimal representation of val
java.math.BigDecimal -- Arbitrary precision decimals.
Copyright (C) 1999, 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.