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.]
Operations on BigDecimal numbers are controlled by a MathContext object, which provides the
context (precision and other information) for the operation. Methods that can take a MathContext
parameter implement the standard arithmetic operators for BigDecimal objects and are known as
operator methods. The default settings provided by the constant MathContext#DEFAULT (digits=9,
form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP) perform general-purpose floating point
arithmetic to nine digits of precision. The MathContext parameter must not be null.
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 form setting.
The names of methods in this class follow the conventions established by java.lang.Number,
java.math.BigInteger, and java.math.BigDecimal in Java 1.1 and Java 1.2.
MathContext
| Modifier and Type | Field and Description |
|---|---|
| private static byte[] | |
| private static byte[] | |
| private int | exp
The exponent. |
| private byte | form
The formatting style. |
| private byte | ind
The indicator. |
| private static final byte | |
| private static final byte | |
| private static final byte | |
| private byte[] | mant
The value of the mantissa. |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| public static final BigDecimal | ONE
The |
| private static final MathContext | |
| public static final int | ROUND_CEILING
Rounding mode to round to a more positive number. |
| public static final int | ROUND_DOWN
Rounding mode to round towards zero. |
| public static final int | ROUND_FLOOR
Rounding mode to round to a more negative number. |
| public static final int | ROUND_HALF_DOWN
Rounding mode to round to nearest neighbor, where an equidistant value is rounded down. |
| public static final int | ROUND_HALF_EVEN
Rounding mode to round to nearest neighbor, where an equidistant value is rounded to the nearest even neighbor. |
| public static final int | ROUND_HALF_UP
Rounding mode to round to nearest neighbor, where an equidistant value is rounded up. |
| public static final int | ROUND_UNNECESSARY
Rounding mode to assert that no rounding is necessary. |
| public static final int | ROUND_UP
Rounding mode to round away from zero. |
| private static final long | |
| public static final BigDecimal | TEN
The |
| public static final BigDecimal | ZERO
The |
| Access | Constructor and Description |
|---|---|
| public | BigDecimal(BigDecimal
The bd)BigDecimal to be translated.Constructs a |
| public | BigDecimal(BigInteger
The bi)BigInteger to be converted.Constructs a |
| public | BigDecimal(BigInteger
The bi, int BigInteger to be converted.The scale)int specifying the scale.Constructs a |
| public | BigDecimal(char[]
The inchars)char[] array containing the number to be converted.Constructs a |
| public | BigDecimal(char[]
The inchars, int char[] array containing the number to be converted.The offset, int int offset into the array of the start of the number to be converted.The length)int length of the number.Constructs a |
| public | BigDecimal(double
The num)double to be converted.Constructs a |
| public | |
| public | |
| public | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public BigDecimal | Returns: ABigDecimal whose value is the absolute value of this BigDecimal.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is the absolute value of this BigDecimal.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: ABigDecimal whose value is this+rhs, using fixed point arithmetic.The rhs)BigDecimal for the right hand side of the addition.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is this+rhs.The rhs, MathContext BigDecimal for the right hand side of the addition.The set)MathContext arithmetic settings.Returns a |
| private static final boolean | |
| private void | |
| private void | |
| private static final byte[] | |
| public byte | |
| private void | |
| private static final BigDecimal | |
| public int | Returns: Anint whose value is -1, 0, or 1 as this is numerically less than, equal to,
or greater than rhs.The rhs)BigDecimal for the right hand side of the comparison.Implements java. BigDecimal to another, using unlimited precision.
|
| public int | Returns: Anint whose value is -1, 0, or 1 as this is numerically less than, equal to,
or greater than rhs.The rhs, MathContext BigDecimal for the right hand side of the comparison.The set)MathContext arithmetic settings.Compares this |
| private static final byte[] | |
| public BigDecimal | Returns: A plainBigDecimal whose value is this/rhs, using fixed point arithmetic.The rhs)BigDecimal for the right hand side of the division.Returns a plain |
| public BigDecimal | Returns: A plainBigDecimal whose value is this/rhs, using fixed point arithmetic and
the specified rounding mode.The rhs, int BigDecimal for the right hand side of the division.The round)int rounding mode to be used for the division (see the MathContext class).Returns a plain |
| public BigDecimal | Returns: A plainBigDecimal whose value is this/rhs, using fixed point arithmetic and
the specified rounding mode.The rhs, int BigDecimal for the right hand side of the division.The scale, int int scale to be used for the result.The round)int rounding mode to be used for the division (see the MathContext class).Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is this/rhs.The rhs, MathContext BigDecimal for the right hand side of the division.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: ABigDecimal whose value is the integer part of this/rhs.The rhs)BigDecimal for the right hand side of the integer division.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is the integer part of this/rhs.The rhs, MathContext BigDecimal for the right hand side of the integer division.The set)MathContext arithmetic settings.Returns a |
| private BigDecimal | |
| public double | Returns: Adouble corresponding to this.Implements abstract java. BigDecimal to a double.
|
| public boolean | Returns: Aboolean whose value true if and only if the operands have identical string
representations.The obj)Object for the right hand side of the comparison.Overrides java. BigDecimal with rhs for equality.
|
| private static final byte[] | |
| private BigDecimal | |
| public float | Returns: Afloat corresponding to this.Implements abstract java. BigDecimal to a float.
|
| public String | Returns: AString representing this BigDecimal, laid out according to the specified parametersThe before, int int specifying the number of places before the decimal point. Use -1 for 'as many as are needed'.The after)int specifying the number of places after the decimal point. Use -1 for 'as many as are needed'.Returns the |
| public String | Returns: AString representing this BigDecimal, laid out according to the specified
parametersThe before, int int specifying the number of places before the decimal point. Use -1 for 'as many as
are needed'.The after, int int specifying the number of places after the decimal point. Use -1 for 'as many as
are needed'.The explaces, int int specifying the number of places to be used for any exponent. Use -1 for 'as many
as are needed'.The exdigits, int int specifying the trigger (digits before the decimal point) which if exceeded causes
exponential notation to be used. Use 0 to force exponential notation. Use -1 to force plain notation
(no exponential notation).The exformint, int int specifying the form of exponential notation to be used (
MathContext#SCIENTIFIC or MathContext#ENGINEERING).The exround)int specifying the rounding mode to use. Use -1 for the default,
MathContext#ROUND_HALF_UP.Returns the |
| public int | Returns: Anint that is the hashcode for this.Overrides java. BigDecimal.
|
| private int | |
| public int | Returns: Anint converted from this, truncated and decapitated if necessary.Implements abstract java. BigDecimal to an int.
|
| public int | |
| private char[] | |
| public long | Returns: Along converted from this, truncated and decapitated if necessary.Implements abstract java. BigDecimal to a long.
|
| public long | |
| public BigDecimal | Returns: ABigDecimal whose value is the maximum of this and rhs.The rhs)BigDecimal for the right hand side of the comparison.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is the maximum of this and rhs.The rhs, MathContext BigDecimal for the right hand side of the comparison.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: ABigDecimal whose value is the minimum of this and rhs.The rhs)BigDecimal for the right hand side of the comparison.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is the minimum of this and rhs.The rhs, MathContext BigDecimal for the right hand side of the comparison.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: ABigDecimal derived from this, with the decimal point moved n
places to the left.The n)int specifying the number of places to move the decimal point leftwards.Returns a plain |
| public BigDecimal | Returns: ABigDecimal derived from this, with the decimal point moved n
places to the right.The n)int specifying the number of places to move the decimal point rightwards.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is this*rhs, using fixed point arithmetic.The rhs)BigDecimal for the right hand side of the multiplication.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is this*rhs.The rhs, MathContext BigDecimal for the right hand side of the multiplication.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | |
| public BigDecimal | Returns: ABigDecimal whose value is -this.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | |
| public BigDecimal | Returns: ABigDecimal whose value is +this.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: ABigDecimal whose value is this**rhs, using fixed point arithmetic.The rhs)BigDecimal for the right hand side of the operation (the power).Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is this**rhs.The rhs, MathContext BigDecimal for the right hand side of the operation (the power).The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: ABigDecimal whose value is the remainder of this/rhs, using fixed point
arithmetic.The rhs)BigDecimal for the right hand side of the remainder operation.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is the remainder of this+rhs.The rhs, MathContext BigDecimal for the right hand side of the remainder operation.The set)MathContext arithmetic settings.Returns a |
| private BigDecimal | |
| private BigDecimal | |
| public int | Returns: Anint whose value is the scale of this BigDecimal.Returns the scale of this |
| public BigDecimal | Returns: A plainBigDecimal with the given scale.The scale)int specifying the scale of the resulting BigDecimal.Returns a plain |
| public BigDecimal | Returns: A plainBigDecimal with the given scale.The scale, int int specifying the scale of the resulting BigDecimal.The round)int rounding mode to be used for the division (see the MathContext class).Returns a plain |
| public short | |
| public int | Returns: Anint which is -1 if the BigDecimal is negative, 0 if it is numerically equal
to zero, or 1 if it is positive.Returns the sign of this |
| public BigDecimal | Returns: ABigDecimal whose value is this-rhs, using fixed point arithmetic.The rhs)BigDecimal for the right hand side of the subtraction.Returns a plain |
| public BigDecimal | Returns: ABigDecimal whose value is this-rhs.The rhs, MathContext BigDecimal for the right hand side of the subtraction.The set)MathContext arithmetic settings.Returns a |
| public BigDecimal | Returns: Thejava.math.BigDecimal equal in value to this BigDecimal.Converts this |
| public BigInteger | Returns: Thejava.math.BigInteger equal in value to the integer part of this BigDecimal.Converts this |
| public BigInteger | Returns: Thejava.math.BigInteger equal in value to the integer part of this BigDecimal.Converts this |
| public char[] | Returns: Thechar[] array corresponding to this BigDecimal.Returns the |
| public String | Returns: TheString exactly corresponding to this BigDecimal.Overrides java. BigDecimal as a String.
|
| public BigInteger | Returns: Thejava.math.BigInteger equal in value to this BigDecimal multiplied by ten to
the power of this.scale().Returns the number as a |
| public static BigDecimal | Returns: TheBigDecimal equal in value to dub.The dub)double to be translated.Translates a |
| public static BigDecimal | Returns: TheBigDecimal equal in value to lint.The lint)long to be translated.Translates a |
| public static BigDecimal | Returns: TheBigDecimal equal in value to lint.The lint, int long to be translated.The scale)int scale to be applied.Translates a |