

PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object java.lang.Number java.math.BigDecimal
public class BigDecimal
Immutable, arbitraryprecision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32bit 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}).
The BigDecimal class provides operations for
arithmetic, scale manipulation, rounding, comparison, hashing, and
format conversion. The toString()
method provides a
canonical representation of a BigDecimal.
The BigDecimal class gives its user complete control
over rounding behavior. If no rounding mode is specified and the
exact result cannot be represented, an exception is thrown;
otherwise, calculations can be carried out to a chosen precision
and rounding mode by supplying an appropriate MathContext
object to the operation. In either case, eight rounding
modes are provided for the control of rounding. Using the
integer fields in this class (such as ROUND_HALF_UP
) to
represent rounding mode is largely obsolete; the enumeration values
of the RoundingMode enum, (such as RoundingMode.HALF_UP
) should be used instead.
When a MathContext object is supplied with a precision
setting of 0 (for example, MathContext.UNLIMITED
),
arithmetic operations are exact, as are the arithmetic methods
which take no MathContext object. (This is the only
behavior that was supported in releases prior to 5.) As a
corollary of computing the exact result, the rounding mode setting
of a MathContext object with a precision setting of 0 is
not used and thus irrelevant. In the case of divide, the exact
quotient could have an infinitely long decimal expansion; for
example, 1 divided by 3. If the quotient has a nonterminating
decimal expansion and the operation is specified to return an exact
result, an ArithmeticException is thrown. Otherwise, the
exact result of the division is returned, as done for other
operations.
When the precision setting is not 0, the rules of BigDecimal arithmetic are broadly compatible with selected modes of operation of the arithmetic defined in ANSI X3.2741996 and ANSI X3.2741996/AM 12000 (section 7.4). Unlike those standards, BigDecimal includes many rounding modes, which were mandatory for division in BigDecimal releases prior to 5. Any conflicts between these ANSI standards and the BigDecimal specification are resolved in favor of BigDecimal.
Since the same numerical value can have different representations (with different scales), the rules of arithmetic and rounding must specify both the numerical result and the scale used in the result's representation.
In general the rounding modes and precision setting determine how operations return results with a limited number of digits when the exact result has more digits (perhaps infinitely many in the case of division) than the number of digits returned. First, the total number of digits to return is specified by the MathContext's precision setting; this determines the result's precision. The digit count starts from the leftmost nonzero digit of the exact result. The rounding mode determines how any discarded trailing digits affect the returned result.
For all arithmetic operators , the operation is carried out as though an exact intermediate result were first calculated and then rounded to the number of digits specified by the precision setting (if necessary), using the selected rounding mode. If the exact result is not returned, some digit positions of the exact result are discarded. When rounding increases the magnitude of the returned result, it is possible for a new digit position to be created by a carry propagating to a leading "9" digit. For example, rounding the value 999.9 to three digits rounding up would be numerically equal to one thousand, represented as 100×10^{1}. In such cases, the new "1" is the leading digit position of the returned result.
Besides a logical exact result, each arithmetic operation has a preferred scale for representing a result. The preferred scale for each operation is listed in the table below.
Operation  Preferred Scale of Result 

Add  max(addend.scale(), augend.scale()) 
Subtract  max(minuend.scale(), subtrahend.scale()) 
Multiply  multiplier.scale() + multiplicand.scale() 
Divide  dividend.scale()  divisor.scale() 
Before rounding, the scale of the logical exact intermediate
result is the preferred scale for that operation. If the exact
numerical result cannot be represented in precision
digits, rounding selects the set of digits to return and the scale
of the result is reduced from the scale of the intermediate result
to the least scale which can represent the precision
digits actually returned. If the exact result can be represented
with at most precision
digits, the representation
of the result with the scale closest to the preferred scale is
returned. In particular, an exactly representable quotient may be
represented in fewer than precision
digits by removing
trailing zeros and decreasing the scale. For example, rounding to
three digits using the floor
rounding mode,
19/100 = 0.19 // integer=19, scale=2
but
21/110 = 0.190 // integer=190, scale=3
Note that for add, subtract, and multiply, the reduction in scale will equal the number of digit positions of the exact result which are discarded. If the rounding causes a carry propagation to create a new highorder digit position, an additional digit of the result is discarded than when no new digit position is created.
Other methods may have slightly different rounding semantics. For example, the result of the pow method using the specified algorithm can occasionally differ from the rounded mathematical result by more than one unit in the last place, one ulp .
Two types of operations are provided for manipulating the scale
of a BigDecimal: scaling/rounding operations and decimal
point motion operations. Scaling/rounding operations (setScale
and round
) return a
BigDecimal whose value is approximately (or exactly) equal
to that of the operand, but whose scale or precision is the
specified value; that is, they increase or decrease the precision
of the stored number with minimal effect on its value. Decimal
point motion operations (movePointLeft
and
movePointRight
) return a
BigDecimal created from the operand by moving the decimal
point a specified distance in the specified direction.
For the sake of brevity and clarity, pseudocode is used throughout the descriptions of BigDecimal methods. The pseudocode expression (i + j) is shorthand for "a BigDecimal whose value is that of the BigDecimal i added to that of the BigDecimal j." The pseudocode expression (i == j) is shorthand for "true if and only if the BigDecimal i represents the same value as the BigDecimal j." Other pseudocode expressions are interpreted similarly. Square brackets are used to represent the particular BigInteger and scale pair defining a BigDecimal value; for example [19, 2] is the BigDecimal numerically equal to 0.19 having a scale of 2.
Note: care should be exercised if BigDecimal objects
are used as keys in a SortedMap
or
elements in a SortedSet
since
BigDecimal's natural ordering is inconsistent
with equals. See Comparable
, SortedMap
or SortedSet
for more
information.
All methods and constructors for this class throw NullPointerException when passed a null object reference for any input parameter.
BigInteger
,
MathContext
,
RoundingMode
,
SortedMap
,
SortedSet
,
Serialized FormField Summary  

static BigDecimal 
ONE
The value 1, with a scale of 0. 
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 "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 "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 value 10, with a scale of 0. 
static BigDecimal 
ZERO
The value 0, with a scale of 0. 
Constructor Summary  

BigDecimal (BigInteger val)
Translates a BigInteger into a BigDecimal. 

BigDecimal (BigInteger unscaledVal,
int scale)
Translates a BigInteger unscaled value and an int scale into a BigDecimal. 

BigDecimal (BigInteger unscaledVal,
int scale,
MathContext mc)
Translates a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to the context settings. 

BigDecimal (BigInteger val,
MathContext mc)
Translates a BigInteger into a BigDecimal rounding according to the context settings. 

BigDecimal (char[] in)
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String)
constructor. 

BigDecimal (char[] in,
int offset,
int len)
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String)
constructor, while allowing a subarray to be specified. 

BigDecimal (char[] in,
int offset,
int len,
MathContext mc)
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String)
constructor, while allowing a subarray to be specified and
with rounding according to the context settings. 

BigDecimal (char[] in,
MathContext mc)
Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String)
constructor and with rounding according to the context
settings. 

BigDecimal (double val)
Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floatingpoint value. 

BigDecimal (double val,
MathContext mc)
Translates a double into a BigDecimal, with rounding according to the context settings. 

BigDecimal (int val)
Translates an int into a BigDecimal. 

BigDecimal (int val,
MathContext mc)
Translates an int into a BigDecimal, with rounding according to the context settings. 

BigDecimal (long val)
Translates a long into a BigDecimal. 

BigDecimal (long val,
MathContext mc)
Translates a long into a BigDecimal, with rounding according to the context settings. 

BigDecimal (String val)
Translates the string representation of a BigDecimal into a BigDecimal. 

BigDecimal (String val,
MathContext mc)
Translates the string representation of a BigDecimal into a BigDecimal, accepting the same strings as the BigDecimal(String) constructor, with rounding
according to the context settings. 
Method Summary  

BigDecimal 
abs ()
Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale(). 
BigDecimal 
abs (MathContext mc)
Returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings. 
BigDecimal 
add (BigDecimal augend)
Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()). 
BigDecimal 
add (BigDecimal augend,
MathContext mc)
Returns a BigDecimal whose value is (this + augend), with rounding according to the context settings. 
byte 
byteValueExact ()
Converts this BigDecimal to a byte, checking for lost information. 
int 
compareTo (BigDecimal val)
Compares this BigDecimal with the specified BigDecimal. 
BigDecimal 
divide (BigDecimal divisor)
Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale()  divisor.scale()); if the exact quotient cannot be represented (because it has a nonterminating decimal expansion) an ArithmeticException is thrown. 
BigDecimal 
divide (BigDecimal divisor,
int roundingMode)
Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale(). 
BigDecimal 
divide (BigDecimal divisor,
int scale,
int roundingMode)
Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. 
BigDecimal 
divide (BigDecimal divisor,
int scale,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. 
BigDecimal 
divide (BigDecimal divisor,
MathContext mc)
Returns a BigDecimal whose value is (this / divisor), with rounding according to the context settings. 
BigDecimal 
divide (BigDecimal divisor,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale(). 
BigDecimal [] 
divideAndRemainder (BigDecimal divisor)
Returns a twoelement BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands. 
BigDecimal [] 
divideAndRemainder (BigDecimal divisor,
MathContext mc)
Returns a twoelement BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands calculated with rounding according to the context settings. 
BigDecimal 
divideToIntegralValue (BigDecimal divisor)
Returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down. 
BigDecimal 
divideToIntegralValue (BigDecimal divisor,
MathContext mc)
Returns a BigDecimal whose value is the integer part of (this / divisor). 
double 
doubleValue ()
Converts this BigDecimal to a double. 
boolean 
equals (Object x)
Compares this BigDecimal with the specified Object for equality. 
float 
floatValue ()
Converts this BigDecimal to a float. 
int 
hashCode ()
Returns the hash code for this BigDecimal. 
int 
intValue ()
Converts this BigDecimal to an int. 
int 
intValueExact ()
Converts this BigDecimal to an int, checking for lost information. 
long 
longValue ()
Converts this BigDecimal to a long. 
long 
longValueExact ()
Converts this BigDecimal to a long, checking for lost information. 
BigDecimal 
max (BigDecimal val)
Returns the maximum of this BigDecimal and val. 
BigDecimal 
min (BigDecimal val)
Returns the minimum of this BigDecimal and val. 
BigDecimal 
movePointLeft (int n)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left. 
BigDecimal 
movePointRight (int n)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. 
BigDecimal 
multiply (BigDecimal multiplicand)
Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()). 
BigDecimal 
multiply (BigDecimal multiplicand,
MathContext mc)
Returns a BigDecimal whose value is (this × multiplicand), with rounding according to the context settings. 
BigDecimal 
negate ()
Returns a BigDecimal whose value is (this), and whose scale is this.scale(). 
BigDecimal 
negate (MathContext mc)
Returns a BigDecimal whose value is (this), with rounding according to the context settings. 
BigDecimal 
plus ()
Returns a BigDecimal whose value is (+this), and whose scale is this.scale(). 
BigDecimal 
plus (MathContext mc)
Returns a BigDecimal whose value is (+this), with rounding according to the context settings. 
BigDecimal 
pow (int n)
Returns a BigDecimal whose value is (this^{n}), The power is computed exactly, to unlimited precision. 
BigDecimal 
pow (int n,
MathContext mc)
Returns a BigDecimal whose value is (this^{n}). 
int 
precision ()
Returns the precision of this BigDecimal. 
BigDecimal 
remainder (BigDecimal divisor)
Returns a BigDecimal whose value is (this % divisor). 
BigDecimal 
remainder (BigDecimal divisor,
MathContext mc)
Returns a BigDecimal whose value is (this % divisor), with rounding according to the context settings. 
BigDecimal 
round (MathContext mc)
Returns a BigDecimal rounded according to the MathContext settings. 
int 
scale ()
Returns the scale of this BigDecimal. 
BigDecimal 
scaleByPowerOfTen (int n)
Returns a BigDecimal whose numerical value is equal to (this * 10^{n}). 
BigDecimal 
setScale (int newScale)
Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's. 
BigDecimal 
setScale (int newScale,
int roundingMode)
Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value. 
BigDecimal 
setScale (int newScale,
RoundingMode roundingMode)
Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value. 
short 
shortValueExact ()
Converts this BigDecimal to a short, checking for lost information. 
int 
signum ()
Returns the signum function of this BigDecimal. 
BigDecimal 
stripTrailingZeros ()
Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. 
BigDecimal 
subtract (BigDecimal subtrahend)
Returns a BigDecimal whose value is (this  subtrahend), and whose scale is max(this.scale(), subtrahend.scale()). 
BigDecimal 
subtract (BigDecimal subtrahend,
MathContext mc)
Returns a BigDecimal whose value is (this  subtrahend), with rounding according to the context settings. 
BigInteger 
toBigInteger ()
Converts this BigDecimal to a BigInteger. 
BigInteger 
toBigIntegerExact ()
Converts this BigDecimal to a BigInteger, checking for lost information. 
String 
toEngineeringString ()
Returns a string representation of this BigDecimal, using engineering notation if an exponent is needed. 
String 
toPlainString ()
Returns a string representation of this BigDecimal without an exponent field. 
String 
toString ()
Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed. 
BigDecimal 
ulp ()
Returns the size of an ulp, a unit in the last place, of this BigDecimal. 
BigInteger 
unscaledValue ()
Returns a BigInteger whose value is the unscaled value of this BigDecimal. 
static BigDecimal 
valueOf (double val)
Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method. 
static BigDecimal 
valueOf (long val)
Translates a long value into a BigDecimal with a scale of zero. 
static BigDecimal 
valueOf (long unscaledVal,
int scale)
Translates a long unscaled value and an int scale into a BigDecimal. 
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(char[] in, int offset, int len)
BigDecimal(String)
constructor, while allowing a subarray to be specified.
Note that if the sequence of characters is already available within a character array, using this constructor is faster than converting the char array to string and using the BigDecimal(String) constructor .
in
 char array that is the source of characters.offset
 first character in the array to inspect.len
 number of characters to consider.
NumberFormatException
 if in is not a valid
representation of a BigDecimal or the defined subarray
is not wholly within in.public BigDecimal(char[] in, int offset, int len, MathContext mc)
BigDecimal(String)
constructor, while allowing a subarray to be specified and
with rounding according to the context settings.
Note that if the sequence of characters is already available within a character array, using this constructor is faster than converting the char array to string and using the BigDecimal(String) constructor .
in
 char array that is the source of characters.offset
 first character in the array to inspect.len
 number of characters to consider..mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.
NumberFormatException
 if in is not a valid
representation of a BigDecimal or the defined subarray
is not wholly within in.public BigDecimal(char[] in)
BigDecimal(String)
constructor.
Note that if the sequence of characters is already available as a character array, using this constructor is faster than converting the char array to string and using the BigDecimal(String) constructor .
in
 char array that is the source of characters.
NumberFormatException
 if in is not a valid
representation of a BigDecimal.public BigDecimal(char[] in, MathContext mc)
BigDecimal(String)
constructor and with rounding according to the context
settings.
Note that if the sequence of characters is already available as a character array, using this constructor is faster than converting the char array to string and using the BigDecimal(String) constructor .
in
 char array that is the source of characters.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.
NumberFormatException
 if in is not a valid
representation of a BigDecimal.public BigDecimal(String val)
The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at least one digit in either the integer or the fraction. The number formed by the sign, the integer and the fraction is referred to as the significand.
The exponent consists of the character 'e'
('\u0075') or 'E' ('\u0045')
followed by one or more decimal digits. The value of the
exponent must lie between Integer.MAX_VALUE
(Integer.MIN_VALUE
+1) and Integer.MAX_VALUE
, inclusive.
More formally, the strings this constructor accepts are described by the following grammar:
 BigDecimalString:
 Sign_{opt} Significand Exponent_{opt}
 Sign:
 +
 
 Significand:
 IntegerPart . FractionPart_{opt}
 . FractionPart
 IntegerPart
 IntegerPart:
 Digits
 FractionPart:
 Digits
 Exponent:
 ExponentIndicator SignedInteger
 ExponentIndicator:
 e
 E
 SignedInteger:
 Sign_{opt} Digits
 Digits:
 Digit
 Digits Digit
 Digit:
 any character for which
Character.isDigit(char)
returns true, including 0, 1, 2 ...
The scale of the returned BigDecimal will be the number of digits in the fraction, or zero if the string contains no decimal point, subject to adjustment for any exponent; if the string contains an exponent, the exponent is subtracted from the scale. The value of the resulting scale must lie between Integer.MIN_VALUE and Integer.MAX_VALUE, inclusive.
The charactertodigit mapping is provided by Character.digit(char, int)
set to convert to radix 10. The
String may not contain any extraneous characters (whitespace,
for example).
Examples:
The value of the returned BigDecimal is equal to
significand × 10^{ exponent}.
For each string on the left, the resulting representation
[BigInteger, scale] is shown on the right.
"0" [0,0] "0.00" [0,2] "123" [123,0] "123" [123,0] "1.23E3" [123,1] "1.23E+3" [123,1] "12.3E+7" [123,6] "12.0" [120,1] "12.3" [123,1] "0.00123" [123,5] "1.23E12" [123,14] "1234.5E4" [12345,5] "0E+7" [0,7] "0" [0,0]
Note: For values other than float and
double NaN and ±Infinity, this constructor is
compatible with the values returned by Float.toString(float)
and Double.toString(double)
. This is generally the preferred
way to convert a float or double into a
BigDecimal, as it doesn't suffer from the unpredictability of
the BigDecimal(double)
constructor.
val
 String representation of BigDecimal.
NumberFormatException
 if val is not a valid
representation of a BigDecimal.public BigDecimal(String val, MathContext mc)
BigDecimal(String)
constructor, with rounding
according to the context settings.
val
 string representation of a BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.
NumberFormatException
 if val is not a valid
representation of a BigDecimal.public BigDecimal(double val)
Notes:
Double.toString(double)
method and then using the
BigDecimal(String)
constructor. To get that result,
use the static valueOf(double)
method.
val
 double value to be converted to
BigDecimal.
NumberFormatException
 if val is infinite or NaN.public BigDecimal(double val, MathContext mc)
The results of this constructor can be somewhat unpredictable
and its use is generally not recommended; see the notes under
the BigDecimal(double)
constructor.
val
 double value to be converted to
BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
RoundingMode is UNNECESSARY.
NumberFormatException
 if val is infinite or NaN.public BigDecimal(BigInteger val)
val
 BigInteger value to be converted to
BigDecimal.public BigDecimal(BigInteger val, MathContext mc)
val
 BigInteger value to be converted to
BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal(BigInteger unscaledVal, int scale)
unscaledVal
 unscaled value of the BigDecimal.scale
 scale of the BigDecimal.public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)
unscaledVal
 unscaled value of the BigDecimal.scale
 scale of the BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal(int val)
val
 int value to be converted to
BigDecimal.public BigDecimal(int val, MathContext mc)
val
 int value to be converted to BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal(long val)
val
 long value to be converted to BigDecimal.public BigDecimal(long val, MathContext mc)
val
 long value to be converted to BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.Method Detail 

public static BigDecimal valueOf(long unscaledVal, int scale)
unscaledVal
 unscaled value of the BigDecimal.scale
 scale of the BigDecimal.
public static BigDecimal valueOf(long val)
val
 value of the BigDecimal.
public static BigDecimal valueOf(double val)
Double.toString(double)
method.
Note: This is generally the preferred way to convert
a double (or float) into a
BigDecimal, as the value returned is equal to that
resulting from constructing a BigDecimal from the
result of using Double.toString(double)
.
val
 double to convert to a BigDecimal.
NumberFormatException
 if val is infinite or NaN.public BigDecimal add(BigDecimal augend)
augend
 value to be added to this BigDecimal.
public BigDecimal add(BigDecimal augend, MathContext mc)
augend
 value to be added to this BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal subtract(BigDecimal subtrahend)
subtrahend
 value to be subtracted from this BigDecimal.
public BigDecimal subtract(BigDecimal subtrahend, MathContext mc)
subtrahend
 value to be subtracted from this BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal multiply(BigDecimal multiplicand)
multiplicand
 value to be multiplied by this BigDecimal.
public BigDecimal multiply(BigDecimal multiplicand, MathContext mc)
multiplicand
 value to be multiplied by this BigDecimal.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
The new divide(BigDecimal, int, RoundingMode)
method
should be used in preference to this legacy method.
divisor
 value by which this BigDecimal is to be divided.scale
 scale of the BigDecimal quotient to be returned.roundingMode
 rounding mode to apply.
ArithmeticException
 if divisor is zero,
roundingMode==ROUND_UNNECESSARY and
the specified scale is insufficient to represent the result
of the division exactly.
IllegalArgumentException
 if roundingMode does not
represent a valid rounding mode.ROUND_UP
,
ROUND_DOWN
,
ROUND_CEILING
,
ROUND_FLOOR
,
ROUND_HALF_UP
,
ROUND_HALF_DOWN
,
ROUND_HALF_EVEN
,
ROUND_UNNECESSARY
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
divisor
 value by which this BigDecimal is to be divided.scale
 scale of the BigDecimal quotient to be returned.roundingMode
 rounding mode to apply.
ArithmeticException
 if divisor is zero,
roundingMode==RoundingMode.UNNECESSARY and
the specified scale is insufficient to represent the result
of the division exactly.public BigDecimal divide(BigDecimal divisor, int roundingMode)
The new divide(BigDecimal, RoundingMode)
method
should be used in preference to this legacy method.
divisor
 value by which this BigDecimal is to be divided.roundingMode
 rounding mode to apply.
ArithmeticException
 if divisor==0, or
roundingMode==ROUND_UNNECESSARY and
this.scale() is insufficient to represent the result
of the division exactly.
IllegalArgumentException
 if roundingMode does not
represent a valid rounding mode.ROUND_UP
,
ROUND_DOWN
,
ROUND_CEILING
,
ROUND_FLOOR
,
ROUND_HALF_UP
,
ROUND_HALF_DOWN
,
ROUND_HALF_EVEN
,
ROUND_UNNECESSARY
public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode)
divisor
 value by which this BigDecimal is to be divided.roundingMode
 rounding mode to apply.
ArithmeticException
 if divisor==0, or
roundingMode==RoundingMode.UNNECESSARY and
this.scale() is insufficient to represent the result
of the division exactly.public BigDecimal divide(BigDecimal divisor)
divisor
 value by which this BigDecimal is to be divided.
ArithmeticException
 if the exact quotient does not have a
terminating decimal expansionpublic BigDecimal divide(BigDecimal divisor, MathContext mc)
divisor
 value by which this BigDecimal is to be divided.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY or
mc.precision == 0 and the quotient has a
nonterminating decimal expansion.public BigDecimal divideToIntegralValue(BigDecimal divisor)
(this.scale() 
divisor.scale())
.
divisor
 value by which this BigDecimal is to be divided.
ArithmeticException
 if divisor==0public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc)
(this.scale()  divisor.scale())
. An
ArithmeticException is thrown if the integer part of
the exact quotient needs more than mc.precision
digits.
divisor
 value by which this BigDecimal is to be divided.mc
 the context to use.
ArithmeticException
 if divisor==0
ArithmeticException
 if mc.precision > 0 and the result
requires a precision of more than mc.precision digits.public BigDecimal remainder(BigDecimal divisor)
The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)). Note that this is not the modulo operation (the result can be negative).
divisor
 value by which this BigDecimal is to be divided.
ArithmeticException
 if divisor==0public BigDecimal remainder(BigDecimal divisor, MathContext mc)
The remainder is given by this.subtract(this.divideToIntegralValue(divisor, mc).multiply(divisor)). Note that this is not the modulo operation (the result can be negative).
divisor
 value by which this BigDecimal is to be divided.mc
 the context to use.
ArithmeticException
 if divisor==0
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY, or mc.precision
> 0 and the result of this.divideToIntgralValue(divisor) would
require a precision of more than mc.precision digits.divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
public BigDecimal [] divideAndRemainder(BigDecimal divisor)
Note that if both the integer quotient and remainder are needed, this method is faster than using the divideToIntegralValue and remainder methods separately because the division need only be carried out once.
divisor
 value by which this BigDecimal is to be divided,
and the remainder computed.
ArithmeticException
 if divisor==0divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
,
remainder(java.math.BigDecimal, java.math.MathContext)
public BigDecimal [] divideAndRemainder(BigDecimal divisor, MathContext mc)
Note that if both the integer quotient and remainder are needed, this method is faster than using the divideToIntegralValue and remainder methods separately because the division need only be carried out once.
divisor
 value by which this BigDecimal is to be divided,
and the remainder computed.mc
 the context to use.
ArithmeticException
 if divisor==0
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY, or mc.precision
> 0 and the result of this.divideToIntgralValue(divisor) would
require a precision of more than mc.precision digits.divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
,
remainder(java.math.BigDecimal, java.math.MathContext)
public BigDecimal pow(int n)
The parameter n must be in the range 0 through
999999999, inclusive. ZERO.pow(0) returns ONE
.
Note that future releases may expand the allowable exponent
range of this method.
n
 power to raise this BigDecimal to.
ArithmeticException
 if n is out of range.public BigDecimal pow(int n, MathContext mc)
The X3.2741996 algorithm is:
ONE
is returned even if
this is zero, otherwise
n
 power to raise this BigDecimal to.mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY, or n is out
of range.public BigDecimal abs()
public BigDecimal abs(MathContext mc)
mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal negate()
public BigDecimal negate(MathContext mc)
mc
 the context to use.
ArithmeticException
 if or the result is inexact but the
rounding mode is UNNECESSARY.public BigDecimal plus()
This method, which simply returns this BigDecimal
is included for symmetry with the unary minus method negate()
.
negate()
public BigDecimal plus(MathContext mc)
The effect of this method is identical to that of the round(MathContext)
method.
mc
 the context to use.
ArithmeticException
 if the result is inexact but the
rounding mode is UNNECESSARY.round(MathContext)
public int signum()
public int scale()
public int precision()
The precision of a zero value is 1.
public BigInteger unscaledValue()
public BigDecimal round(MathContext mc)
The effect of this method is identical to that of the
plus(MathContext)
method.
mc
 the context to use.
ArithmeticException
 if the rounding mode is
UNNECESSARY and the
BigDecimal operation would require rounding.plus(MathContext)
public BigDecimal setScale(int newScale, RoundingMode roundingMode)
newScale
 scale of the BigDecimal value to be returned.roundingMode
 The rounding mode to apply.
ArithmeticException
 if roundingMode==UNNECESSARY
and the specified scaling operation would require
rounding.RoundingMode
public BigDecimal setScale(int newScale, int roundingMode)
Note that since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated.
The new setScale(int, RoundingMode)
method should
be used in preference to this legacy method.
newScale
 scale of the BigDecimal value to be returned.roundingMode
 The rounding mode to apply.
ArithmeticException
 if roundingMode==ROUND_UNNECESSARY
and the specified scaling operation would require
rounding.
IllegalArgumentException
 if roundingMode does not
represent a valid rounding mode.ROUND_UP
,
ROUND_DOWN
,
ROUND_CEILING
,
ROUND_FLOOR
,
ROUND_HALF_UP
,
ROUND_HALF_DOWN
,
ROUND_HALF_EVEN
,
ROUND_UNNECESSARY
public BigDecimal setScale(int newScale)
This call is typically used to increase the scale, in which case it is guaranteed that there exists a BigDecimal of the specified scale and the correct value. The call can also be used to reduce the scale if the caller knows that the BigDecimal has sufficiently many zeros at the end of its fractional part (i.e., factors of ten in its integer value) to allow for the rescaling without changing its value.
This method returns the same result as the twoargument versions of setScale, but saves the caller the trouble of specifying a rounding mode in cases where it is irrelevant.
Note that since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated.
newScale
 scale of the BigDecimal value to be returned.
ArithmeticException
 if the specified scaling operation would
require rounding.setScale(int, int)
,
setScale(int, RoundingMode)
public BigDecimal movePointLeft(int n)
n
 number of places to move the decimal point to the left.
ArithmeticException
 if scale overflows.public BigDecimal movePointRight(int n)
n
 number of places to move the decimal point to the right.
ArithmeticException
 if scale overflows.public BigDecimal scaleByPowerOfTen(int n)
ArithmeticException
 if the scale would be
outside the range of a 32bit integer.public BigDecimal stripTrailingZeros()
public int compareTo(BigDecimal val)
compareTo
in interface Comparable <BigDecimal >
val
 BigDecimal to which this BigDecimal is
to be compared.
public boolean equals(Object x)
compareTo
, this method considers two
BigDecimal objects equal only if they are equal in
value and scale (thus 2.0 is not equal to 2.00 when compared by
this method).
equals
in class Object
x
 Object to which this BigDecimal is
to be compared.
compareTo(java.math.BigDecimal)
,
hashCode()
public BigDecimal min(BigDecimal val)
val
 value with which the minimum is to be computed.
compareTo
method, this is returned.compareTo(java.math.BigDecimal)
public BigDecimal max(BigDecimal val)
val
 value with which the maximum is to be computed.
compareTo
method, this is returned.compareTo(java.math.BigDecimal)
public int hashCode()
hashCode
in class Object
equals(Object)
public String toString()
A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used).
Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, scale+(ulength1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).
If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to 6, the number will be converted to a character form without using exponential notation. In this case, if the scale is zero then no decimal point is added and if the scale is positive a decimal point will be inserted with the scale specifying the number of characters to the right of the decimal point. '0' characters are added to the left of the converted unscaled value as necessary. If no character precedes the decimal point after this insertion then a conventional '0' character is prefixed.
Otherwise (that is, if the scale is negative, or the adjusted exponent is less than 6), the number will be converted to a character form using exponential notation. In this case, if the converted BigInteger has more than one digit a decimal point is inserted after the first digit. An exponent in character form is then suffixed to the converted unscaled value (perhaps with inserted decimal point); this comprises the letter 'E' followed immediately by the adjusted exponent converted to a character form. The latter is in base ten, using the characters '0' through '9' with no leading zeros, and is always prefixed by a sign character '' ('\u002D') if the adjusted exponent is negative, '+' ('\u002B') otherwise).
Finally, the entire string is prefixed by a minus sign character '' ('\u002D') if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive.
Examples:
For each representation [unscaled value, scale] on the left, the resulting string is shown on the right.
[123,0] "123" [123,0] "123" [123,1] "1.23E+3" [123,3] "1.23E+5" [123,1] "12.3" [123,5] "0.00123" [123,10] "1.23E8" [123,12] "1.23E10"Notes:
BigDecimal(String)
constructor, then the original
value will be recovered.
NumberFormat
class and its subclasses.
toEngineeringString()
method may be used for
presenting numbers with exponents in engineering notation, and the
setScale
method may be used for
rounding a BigDecimal so it has a known number of digits after
the decimal point.
toString
in class Object
Character.forDigit(int, int)
,
BigDecimal(java.lang.String)
public String toEngineeringString()
Returns a string that represents the BigDecimal as
described in the toString()
method, except that if
exponential notation is used, the power of ten is adjusted to
be a multiple of three (engineering notation) such that the
integer part of nonzero values will be in the range 1 through
999. If exponential notation is used for zero values, a
decimal point and one or two fractional zero digits are used so
that the scale of the zero value is preserved. Note that
unlike the output of toString()
, the output of this
method is not guaranteed to recover the same [integer,
scale] pair of this BigDecimal if the output string is
converting back to a BigDecimal using the string constructor . The result of this method meets
the weaker constraint of always producing a numerically equal
result from applying the string constructor to the method's output.
public String toPlainString()
toString()
,
toEngineeringString()
public BigInteger toBigInteger()
To have an exception thrown if the conversion is inexact (in
other words if a nonzero fractional part is discarded), use the
toBigIntegerExact()
method.
public BigInteger toBigIntegerExact()
ArithmeticException
 if this has a nonzero
fractional part.public long longValue()
longValue
in class Number
public long longValueExact()
ArithmeticException
 if this has a nonzero
fractional part, or will not fit in a long.public int intValue()
intValue
in class Number
public int intValueExact()
ArithmeticException
 if this has a nonzero
fractional part, or will not fit in an int.public short shortValueExact()
ArithmeticException
 if this has a nonzero
fractional part, or will not fit in a short.public byte byteValueExact()
ArithmeticException
 if this has a nonzero
fractional part, or will not fit in a byte.public float floatValue()
Float.NEGATIVE_INFINITY
or Float.POSITIVE_INFINITY
as appropriate. Note that even when
the return value is finite, this conversion can lose
information about the precision of the BigDecimal
value.
floatValue
in class Number
public double doubleValue()
Double.NEGATIVE_INFINITY
or Double.POSITIVE_INFINITY
as appropriate. Note that even when
the return value is finite, this conversion can lose
information about the precision of the BigDecimal
value.
doubleValue
in class Number
public BigDecimal ulp()
this
so the result
for zero and nonzero values is equal to [1,
this.scale()]
.


PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 