|
||||||||||
| 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
The BigDecimal class implements immutable
arbitrary-precision decimal numbers. The methods of the
BigDecimal class provide operations for fixed and
floating point arithmetic, comparison, format conversions, and
hashing.
As the numbers are decimal, there is an exact correspondence between
an instance of a BigDecimal object and its
String representation; the BigDecimal class
provides direct conversions to and from String and
character array (char[]) objects, as well as conversions
to and from the Java primitive types (which may not be exact) and
BigInteger.
In the descriptions of constructors and methods in this documentation,
the value of a BigDecimal number object is shown as the
result of invoking the toString() method on the object.
The internal representation of a decimal number is neither defined
nor exposed, and is not permitted to affect the result of any
operation.
The floating point arithmetic provided by this class is defined by
the ANSI X3.274-1996 standard, and is also documented at
http://www2.hursley.ibm.com/decimal
[This URL will change.]
Each operator method also has a version provided which does
not take a MathContext parameter. For this version of
each method, the context settings used are digits=0,
form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP;
these settings perform fixed point arithmetic with unlimited
precision, as defined for the original BigDecimal class in Java 1.1
and Java 1.2.
For monadic operators, only the optional MathContext
parameter is present; the operation acts upon the current object.
For dyadic operators, a BigDecimal parameter is always
present; it must not be null.
The operation acts with the current object being the left-hand operand
and the BigDecimal parameter being the right-hand operand.
For example, adding two BigDecimal objects referred to
by the names award and extra could be
written as any of:
award.add(extra)
award.add(extra, MathContext.DEFAULT)
award.add(extra, acontext)
(where acontext is a MathContext object),
which would return a BigDecimal object whose value is
the result of adding award and extra under
the appropriate context settings.
When a BigDecimal operator method is used, a set of
rules define what the result will be (and, by implication, how the
result would be represented as a character string).
These rules are defined in the BigDecimal arithmetic documentation
(see the URL above), but in summary:
MathContext parameter for an operation
were MathContext.DEFAULT then the result would be
rounded to 9 digits; the division of 2 by 3 would then result in
0.666666667.
MathContext object. This lets you
calculate using as many digits as you need -- thousands, if necessary.
Fixed point (scaled) arithmetic is indicated by using a
digits setting of 0 (or omitting the
MathContext parameter).
form setting
is not PLAIN), a zero result is always expressed as the
single digit '0' (that is, with no sign, decimal point,
or exponent part).
new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40"
new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40"
new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80"
new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"
where the value on the right of the => would be the
result of the operation, expressed as a String, and
def (in this and following examples) refers to
MathContext.DEFAULT).
This preservation of trailing zeros is desirable for most
calculations (including financial calculations).
If necessary, trailing zeros may be easily removed using division by 1.
digits
(the default is 9 digits).
If the number of places needed before the decimal point exceeds the
digits setting, or the absolute value of the number is
less than 0.000001, then the number will be expressed in
exponential notation; thus
new BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)
results in 1E+12 instead of
1000000000000, and
new BigDecimal("1").divide(new BigDecimal("3E+10"), def)
results in 3.33333333E-11 instead of
0.0000000000333333333.
The form of the exponential notation (scientific or engineering) is
determined by the
The names of methods in this class follow the conventions established
by
Constructs a
The
Constructs a
The
Constructs a
Note that this constructor it an exact conversion; it does not give
the same result as converting
Constructs a
In summary, numbers in
Some valid strings from which a
(Exponential notation means that the number includes an optional
sign and a power of ten following an 'E' that
indicates how the decimal point will be shifted. Thus the
The
Any digits in the parameter must be decimal; that is,
The length of the decimal part (the scale) of the result will
be
The length of the decimal part (the scale) of the result will be
the maximum of the scales of the two operands.
The same as
The length of the decimal part (the scale) of the result will
therefore be the same as the scale of the current object, if the
latter were formatted without exponential notation.
The length of the decimal part (the scale) of the result will be
the same as the scale of the current object, if the latter were
formatted without exponential notation.
The length of the decimal part (the scale) of the result will be
the sum of the scales of the operands, if they were formatted
without exponential notation.
The length of the decimal part (the scale) of the result will be
be
The length of the decimal part (the scale) of the result will be
the maximum of the scales of the two operands.
If the parameter is
The double produced is identical to result of expressing the
If the parameter is
"Exactly equal", here, means that the
The float produced is identical to result of expressing the
Note that two
If the given scale (which must be zero or positive) is the same as
or greater than the length of the decimal part (the scale) of this
If the given scale is less than the length of the decimal part (the
scale) of this
The same as
If the given scale (which must be zero or positive) is the same as
or greater than the length of the decimal part (the scale) of this
If the given scale is less than the length of the decimal part (the
scale) of this
If
Any decimal part is truncated (discarded).
By definition, using the
The result is given by:
A
form setting.
java.lang.Number, java.math.BigInteger,
and java.math.BigDecimal in Java 1.1 and Java 1.2.
Field Summary
static intROUND_CEILING
Rounding mode to round to a more positive number.
static intROUND_DOWN
Rounding mode to round towards zero.
static intROUND_FLOOR
Rounding mode to round to a more negative number.
static intROUND_HALF_DOWN
Rounding mode to round to nearest neighbor, where an equidistant
value is rounded down.
static intROUND_HALF_EVEN
Rounding mode to round to nearest neighbor, where an equidistant
value is rounded to the nearest even neighbor.
static intROUND_HALF_UP
Rounding mode to round to nearest neighbor, where an equidistant
value is rounded up.
static intROUND_UNNECESSARY
Rounding mode to assert that no rounding is necessary.
static intROUND_UP
Rounding mode to round away from zero.
Constructor Summary
BigDecimal(BigInteger bi)
Constructs a BigDecimal object from a
BigInteger, with scale 0.
BigDecimal(BigInteger bi,
int scale)
Constructs a BigDecimal object from a
BigInteger and a scale.
BigDecimal(double num)
Constructs a BigDecimal object directly from a
double.
BigDecimal(String string)
Constructs a BigDecimal object from a String.
Method Summary
BigDecimalabs()
Returns a plain BigDecimal whose value is the absolute
value of this BigDecimal.
BigDecimaladd(BigDecimal rhs)
Returns a plain BigDecimal whose value is
this+rhs, using fixed point arithmetic.
bytebyteValue()
intcompareTo(BigDecimal rhs)
Compares this BigDecimal to another, using unlimited
precision.
intcompareTo(Object rhsobj)
Compares this BigDecimal with the value of the parameter.
BigDecimaldivide(BigDecimal rhs,
int round)
Returns a plain BigDecimal whose value is
this/rhs, using fixed point arithmetic and a
rounding mode.
BigDecimaldivide(BigDecimal rhs,
int scale,
int round)
Returns a plain BigDecimal whose value is
this/rhs, using fixed point arithmetic and a
given scale and rounding mode.
doubledoubleValue()
Converts this BigDecimal to a double.
booleanequals(Object obj)
Compares this BigDecimal with rhs for
equality.
floatfloatValue()
Converts this BigDecimal to a float.
inthashCode()
Returns the hashcode for this BigDecimal.
intintValue()
Converts this BigDecimal to an int.
longlongValue()
Converts this BigDecimal to a long.
BigDecimalmax(BigDecimal rhs)
Returns a plain BigDecimal whose value is
the maximum of this and rhs.
BigDecimalmin(BigDecimal rhs)
Returns a plain BigDecimal whose value is
the minimum of this and rhs.
BigDecimalmovePointLeft(int n)
Returns a plain BigDecimal whose decimal point has
been moved to the left by a specified number of positions.
BigDecimalmovePointRight(int n)
Returns a plain BigDecimal whose decimal point has
been moved to the right by a specified number of positions.
BigDecimalmultiply(BigDecimal rhs)
Returns a plain BigDecimal whose value is
this*rhs, using fixed point arithmetic.
BigDecimalnegate()
Returns a plain BigDecimal whose value is
-this.
intscale()
Returns the scale of this BigDecimal.
BigDecimalsetScale(int scale)
Returns a plain BigDecimal with a given scale.
BigDecimalsetScale(int scale,
int round)
Returns a plain BigDecimal with a given scale.
shortshortValue()
intsignum()
Returns the sign of this BigDecimal, as an
int.
BigDecimalsubtract(BigDecimal rhs)
Returns a plain BigDecimal whose value is
this-rhs, using fixed point arithmetic.
BigIntegertoBigInteger()
Converts this BigDecimal to a
java.math.BigInteger.
StringtoString()
Returns the BigDecimal as a String.
BigIntegerunscaledValue()
Returns the number as a BigInteger after removing the
scale.
static BigDecimalvalueOf(long lint)
Translates a long to a BigDecimal.
static BigDecimalvalueOf(long lint,
int scale)
Translates a long to a BigDecimal with a
given scale.
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Field Detail
ROUND_CEILING
public static final int ROUND_CEILING
ROUND_DOWN
public static final int ROUND_DOWN
ROUND_FLOOR
public static final int ROUND_FLOOR
ROUND_HALF_DOWN
public static final int ROUND_HALF_DOWN
ROUND_HALF_EVEN
public static final int ROUND_HALF_EVEN
ROUND_HALF_UP
public static final int ROUND_HALF_UP
ROUND_UNNECESSARY
public static final int ROUND_UNNECESSARY
ROUND_UP
public static final int ROUND_UP
Constructor Detail
BigDecimal
public BigDecimal(BigInteger bi)
BigDecimal object from a
BigInteger, with scale 0.
BigDecimal which is the exact decimal
representation of the BigInteger, with a scale of
zero.
The value of the BigDecimal is identical to the value
of the BigInteger.
The parameter must not be null.
BigDecimal will contain only decimal digits,
prefixed with a leading minus sign (hyphen) if the
BigInteger is negative. A leading zero will be
present only if the BigInteger is zero.
bi - The BigInteger to be converted.
BigDecimal
public BigDecimal(BigInteger bi,
int scale)
BigDecimal object from a
BigInteger and a scale.
BigDecimal which is the exact decimal
representation of the BigInteger, scaled by the
second parameter, which may not be negative.
The value of the BigDecimal is the
BigInteger divided by ten to the power of the scale.
The BigInteger parameter must not be
null.
BigDecimal will contain only decimal digits, (with
an embedded decimal point followed by scale decimal
digits if the scale is positive), prefixed with a leading minus
sign (hyphen) if the BigInteger is negative. A
leading zero will be present only if the BigInteger is
zero.
bi - The BigInteger to be converted.scale - The int specifying the scale.
NumberFormatException - if the scale is negative.
BigDecimal
public BigDecimal(double num)
BigDecimal object directly from a
double.
BigDecimal which is the exact decimal
representation of the 64-bit signed binary floating point
parameter.
num to a
String using the Double.toString() method
and then using the BigDecimal(java.lang.String)
constructor.
num - The double to be converted.
NumberFormatException - if the parameter is infinite or
not a number.
BigDecimal
public BigDecimal(String string)
BigDecimal object from a String.
BigDecimal from the parameter, which must
not be null and must represent a valid number,
as described formally in the documentation referred to
above.
String form must have at least
one digit, may have a leading sign, may have a decimal point, and
exponential notation may be used. They follow conventional syntax,
and may not contain blanks.
BigDecimal might
be constructed are:
"0" -- Zero
"12" -- A whole number
"-76" -- A signed whole number
"12.70" -- Some decimal places
"+0.003" -- Plus sign is allowed
"17." -- The same as 17
".5" -- The same as 0.5
"4E+9" -- Exponential notation
"0.73e-7" -- Exponential notation
"4E+9" above is just a short way of writing
4000000000, and the "0.73e-7" is short
for 0.000000073.)
BigDecimal constructed from the String is in a
standard form, with no blanks, as though the
add(BigDecimal) method had been used to add zero to the
number with unlimited precision.
If the string uses exponential notation (that is, includes an
e or an E), then the
BigDecimal number will be expressed in scientific
notation (where the power of ten is adjusted so there is a single
non-zero digit to the left of the decimal point); in this case if
the number is zero then it will be expressed as the single digit 0,
and if non-zero it will have an exponent unless that exponent would
be 0. The exponent must fit in nine digits both before and after it
is expressed in scientific notation.
Character.digit(c, 10) (where c is the
character in question) would not return -1.
string - The String to be converted.
NumberFormatException - if the parameter is not a valid
number.
Method Detail
abs
public BigDecimal abs()
BigDecimal whose value is the absolute
value of this BigDecimal.
this.scale()
BigDecimal whose value is the absolute
value of this BigDecimal.
add
public BigDecimal add(BigDecimal rhs)
BigDecimal whose value is
this+rhs, using fixed point arithmetic.
rhs - The BigDecimal for the right hand side of
the addition.
BigDecimal whose value is
this+rhs, using fixed point arithmetic.
compareTo
public int compareTo(BigDecimal rhs)
BigDecimal to another, using unlimited
precision.
rhs - The BigDecimal for the right hand side of
the comparison.
int whose value is -1, 0, or 1 as
this is numerically less than, equal to,
or greater than rhs.compareTo(Object)
divide
public BigDecimal divide(BigDecimal rhs,
int round)
BigDecimal whose value is
this/rhs, using fixed point arithmetic and a
rounding mode.
divide(BigDecimal, int, int),
where the BigDecimal is rhs,
and the second parameter is this.scale(), and
the third is round.
rhs - The BigDecimal for the right hand side of
the division.round - The int rounding mode to be used for
the division.
BigDecimal whose value is
this/rhs, using fixed point arithmetic
and the specified rounding mode.
IllegalArgumentException - if round is not a
valid rounding mode.
ArithmeticException - if rhs is zero.
ArithmeticException - if round is ROUND_UNNECESSARY and
this.scale() is insufficient to
represent the result exactly.
divide
public BigDecimal divide(BigDecimal rhs,
int scale,
int round)
BigDecimal whose value is
this/rhs, using fixed point arithmetic and a
given scale and rounding mode.
rhs - The BigDecimal for the right hand side of
the division.scale - The int scale to be used for the result.round - The int rounding mode to be used for
the division.
BigDecimal whose value is
this/rhs, using fixed point arithmetic
and the specified rounding mode.
IllegalArgumentException - if round is not a
valid rounding mode.
ArithmeticException - if rhs is zero.
ArithmeticException - if scale is negative.
ArithmeticException - if round is ROUND_UNNECESSARY and scale
is insufficient to represent the result exactly.
max
public BigDecimal max(BigDecimal rhs)
BigDecimal whose value is
the maximum of this and rhs.
rhs - The BigDecimal for the right hand side of
the comparison.
BigDecimal whose value is
the maximum of this and rhs.
min
public BigDecimal min(BigDecimal rhs)
BigDecimal whose value is
the minimum of this and rhs.
rhs - The BigDecimal for the right hand side of
the comparison.
BigDecimal whose value is
the minimum of this and rhs.
multiply
public BigDecimal multiply(BigDecimal rhs)
BigDecimal whose value is
this*rhs, using fixed point arithmetic.
rhs - The BigDecimal for the right hand side of
the multiplication.
BigDecimal whose value is
this*rhs, using fixed point arithmetic.
negate
public BigDecimal negate()
BigDecimal whose value is
-this.
this.scale()
BigDecimal whose value is
-this.
subtract
public BigDecimal subtract(BigDecimal rhs)
BigDecimal whose value is
this-rhs, using fixed point arithmetic.
rhs - The BigDecimal for the right hand side of
the subtraction.
BigDecimal whose value is
this-rhs, using fixed point arithmetic.
byteValue
public byte byteValue()
compareTo
public int compareTo(Object rhsobj)
BigDecimal with the value of the parameter.
null, or is not an instance of the
BigDecimal type, an exception is thrown.
Otherwise, the parameter is cast to type BigDecimal
and the result of the compareTo(BigDecimal) method,
using the cast parameter, is returned.
compareTo in interface Comparable
rhsobj - The Object for the right hand side of
the comparison.
int whose value is -1, 0, or 1 as
this is numerically less than, equal to,
or greater than rhs.
ClassCastException - if rhs cannot be cast to
a BigDecimal object.compareTo(BigDecimal)
doubleValue
public double doubleValue()
BigDecimal to a double.
If the BigDecimal is out of the possible range for a
double (64-bit signed floating point) result then an
ArithmeticException is thrown.
BigDecimal as a String and then
converting it using the Double(String) constructor;
this can result in values of Double.NEGATIVE_INFINITY
or Double.POSITIVE_INFINITY.
doubleValue in class Number
double corresponding to this.
equals
public boolean equals(Object obj)
BigDecimal with rhs for
equality.
null, or is not an instance of the
BigDecimal type, or is not exactly equal to the current
BigDecimal object, then false is returned.
Otherwise, true is returned.
String
representations of the BigDecimal numbers are
identical (they have the same characters in the same sequence).
obj - The Object for the right hand side of
the comparison.
boolean whose value true if and
only if the operands have identical string representations.
ClassCastException - if rhs cannot be cast to
a BigDecimal object.compareTo(Object),
compareTo(BigDecimal)
floatValue
public float floatValue()
BigDecimal to a float.
If the BigDecimal is out of the possible range for a
float (32-bit signed floating point) result then an
ArithmeticException is thrown.
BigDecimal as a String and then
converting it using the Float(String) constructor;
this can result in values of Float.NEGATIVE_INFINITY
or Float.POSITIVE_INFINITY.
floatValue in class Number
float corresponding to this.
hashCode
public int hashCode()
BigDecimal.
This hashcode is suitable for use by the
java.util.Hashtable class.
BigDecimal objects are only guaranteed
to produce the same hashcode if they are exactly equal (that is,
the String representations of the
BigDecimal numbers are identical -- they have the same
characters in the same sequence).
int that is the hashcode for this.
intValue
public int intValue()
BigDecimal to an int.
If the BigDecimal has a non-zero decimal part it is
discarded. If the BigDecimal is out of the possible
range for an int (32-bit signed integer) result then
only the low-order 32 bits are used. (That is, the number may be
decapitated.)
int converted from this,
truncated and decapitated if necessary.
longValue
public long longValue()
BigDecimal to a long.
If the BigDecimal has a non-zero decimal part it is
discarded. If the BigDecimal is out of the possible
range for a long (64-bit signed integer) result then
only the low-order 64 bits are used. (That is, the number may be
decapitated.)
long converted from this,
truncated and decapitated if necessary.
movePointLeft
public BigDecimal movePointLeft(int n)
BigDecimal whose decimal point has
been moved to the left by a specified number of positions.
The parameter, n, specifies the number of positions to
move the decimal point.
That is, if n is 0 or positive, the number returned is
given by:
this.multiply(TEN.pow(new BigDecimal(-n)))
n may be negative, in which case the method returns
the same result as movePointRight(-n).
n - The int specifying the number of places to
move the decimal point leftwards.
BigDecimal derived from
this, with the decimal point moved
n places to the left.
movePointRight
public BigDecimal movePointRight(int n)
BigDecimal whose decimal point has
been moved to the right by a specified number of positions.
The parameter, n, specifies the number of positions to
move the decimal point.
That is, if n is 0 or positive, the number returned is
given by:
this.multiply(TEN.pow(new BigDecimal(n)))
n may be negative, in which case the method returns
the same result as movePointLeft(-n).
n - The int specifying the number of places to
move the decimal point rightwards.
BigDecimal derived from
this, with the decimal point moved
n places to the right.
scale
public int scale()
BigDecimal.
Returns a non-negative int which is the scale of the
number. The scale is the number of digits in the decimal part of
the number if the number were formatted without exponential
notation.
int whose value is the scale of this
BigDecimal.
setScale
public BigDecimal setScale(int scale)
BigDecimal with a given scale.
BigDecimal then trailing zeros will be added to the
decimal part as necessary.
BigDecimal then trailing digits
will be removed, and in this case an
ArithmeticException is thrown if any discarded digits
are non-zero.
setScale(int, int), where the first parameter
is the scale, and the second is
MathContext.ROUND_UNNECESSARY.
scale - The int specifying the scale of the
resulting BigDecimal.
BigDecimal with the given scale.
ArithmeticException - if scale is negative.
ArithmeticException - if reducing scale would discard
non-zero digits.
setScale
public BigDecimal setScale(int scale,
int round)
BigDecimal with a given scale.
BigDecimal then trailing zeros will be added to the
decimal part as necessary.
BigDecimal then trailing digits
will be removed, and the rounding mode given by the second
parameter is used to determine if the remaining digits are
affected by a carry.
In this case, an IllegalArgumentException is thrown if
round is not a valid rounding mode.
round is MathContext.ROUND_UNNECESSARY,
an ArithmeticException is thrown if any discarded
digits are non-zero.
scale - The int specifying the scale of the
resulting BigDecimal.round - The int rounding mode to be used for
the division.
BigDecimal with the given scale.
IllegalArgumentException - if round is not a
valid rounding mode.
ArithmeticException - if scale is negative.
ArithmeticException - if round is
MathContext.ROUND_UNNECESSARY, and
reducing scale would discard non-zero digits.
shortValue
public short shortValue()
shortValue in class Number
signum
public int signum()
BigDecimal, as an
int.
This returns the signum function value that represents the
sign of this BigDecimal.
That is, -1 if the BigDecimal is negative, 0 if it is
numerically equal to zero, or 1 if it is positive.
int which is -1 if the
BigDecimal is negative, 0 if it is
numerically equal to zero, or 1 if it is positive.
toBigInteger
public BigInteger toBigInteger()
BigDecimal to a
java.math.BigInteger.
java.math.BigInteger equal in value
to the integer part of this BigDecimal.
toString
public String toString()
BigDecimal as a String.
This returns a String that exactly represents this
BigDecimal, as defined in the decimal documentation
(see class header).
BigDecimal(String) constructor
on the result String will create a
BigDecimal that is exactly equal to the original
BigDecimal.
String exactly corresponding to this
BigDecimal.
unscaledValue
public BigInteger unscaledValue()
BigInteger after removing the
scale.
That is, the number is expressed as a plain number, any decimal
point is then removed (retaining the digits of any decimal part),
and the result is then converted to a BigInteger.
java.math.BigInteger equal in value to
this BigDecimal multiplied by ten to the
power of this.scale().
valueOf
public static BigDecimal valueOf(long lint)
long to a BigDecimal.
That is, returns a plain BigDecimal whose value is
equal to the given long.
lint - The long to be translated.
BigDecimal equal in value to
lint.
valueOf
public static BigDecimal valueOf(long lint,
int scale)
long to a BigDecimal with a
given scale.
That is, returns a plain BigDecimal whose unscaled
value is equal to the given long, adjusted by the
second parameter, scale.
(new BigDecimal(lint)).divide(TEN.pow(new BigDecimal(scale)))
NumberFormatException is thrown if scale
is negative.
lint - The long to be translated.scale - The int scale to be applied.
BigDecimal equal in value to
lint.
NumberFormatException - if the scale is negative.
Overview
Package
Class
Tree
Deprecated
Experimental
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD