站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Java(TM) 2 Platform, Standard Edition, v1.2.2 API Specification: Class BigDecimal - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

JavaTM 2 Platform
Standard Edition

java.math
Class BigDecimal

java.lang.Object
  |
  +--java.lang.Number
        |
        +--java.math.BigDecimal

public class BigDecimal
extends Number
implements Comparable

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a non-negative 32-bit integer scale, which represents the number of digits to the right of the decimal point. The number represented by the BigDecimal is (unscaledValue/10scale). BigDecimal provides operations for basic arithmetic, scale manipulation, comparison, hashing, and format conversion.

The BigDecimal class gives its user complete control over rounding behavior, forcing the user to explicitly specify a rounding behavior for operations capable of discarding precision (divide and setScale). Eight rounding modes are provided for this purpose.

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) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale is the specified value; that is, they increase or decrease the precision of the 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; that is, they change a number's value without affecting its precision.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigDecimal methods. The pseudo-code expression (i + j) is shorthand for "a BigDecimal whose value is that of the BigDecimal i plus that of the BigDecimal j." The pseudo-code expression (i == j) is shorthand for "true if and only if the BigDecimal i represents the same value as the the BigDecimal j." Other pseudo-code expressions are interpreted similarly.

Note: care should be exercised if BigDecimals are to be used as keys in a SortedMap or elements in a SortedSet, as BigDecimal's natural ordering is inconsistent with equals. See Comparable, SortedMap or SortedSet for more information.

See Also:
BigInteger, SortedMap, SortedSet, Serialized Form

Field Summary
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.
 
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(double val)
          Translates a double into a BigDecimal.
BigDecimal(String val)
          Translates the String representation of a BigDecmal into a BigDecimal.
 
Method Summary
 BigDecimal abs()
          Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().
 BigDecimal add(BigDecimal val)
          Returns a BigDecimal whose value is (this + val), and whose scale is max(this.scale(), val.scale()).
 int compareTo(BigDecimal val)
          Compares this BigDecimal with the specified BigDecimal.
 int compareTo(Object o)
          Compares this BigDecimal with the specified Object.
 BigDecimal divide(BigDecimal val, int roundingMode)
          Returns a BigDecimal whose value is (this / val), and whose scale is this.scale().
 BigDecimal divide(BigDecimal val, int scale, int roundingMode)
          Returns a BigDecimal whose value is (this / val), and whose scale is as specified.
 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.
 long longValue()
          Converts this BigDecimal to a long.
 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)
          Moves the decimal point the specified number of places to the right.
 BigDecimal multiply(BigDecimal val)
          Returns a BigDecimal whose value is (this * val), and whose scale is (this.scale() + val.scale()).
 BigDecimal negate()
          Returns a BigDecimal whose value is (-this), and whose scale is this.scale().
 int scale()
          Returns the scale of this BigDecimal.
 BigDecimal setScale(int scale)
          Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's.
 BigDecimal setScale(int scale, 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.
 int signum()
          Returns the signum function of this BigDecimal.
 BigDecimal subtract(BigDecimal val)
          Returns a BigDecimal whose value is (this - val), and whose scale is max(this.scale(), val.scale()).
 BigInteger toBigInteger()
          Converts this BigDecimal to a BigInteger.
 String toString()
          Returns the string representation of this BigDecimal.
 BigInteger unscaledValue()
          Returns a BigInteger whose value is the unscaled value of this BigDecimal.
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

ROUND_UP

public static final int ROUND_UP
Rounding mode to round away from zero. Always increments the digit prior to a non-zero discarded fraction. Note that this rounding mode never decreases the magnitude of the calculated value.

ROUND_DOWN

public static final int ROUND_DOWN
Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates). Note that this rounding mode never increases the magnitude of the calculated value.

ROUND_CEILING

public static final int ROUND_CEILING
Rounding mode to round towards positive infinity. If the BigDecimal is positive, behaves as for ROUND_UP; if negative, behaves as for ROUND_DOWN. Note that this rounding mode never decreases the calculated value.

ROUND_FLOOR

public static final int ROUND_FLOOR
Rounding mode to round towards negative infinity. If the BigDecimal is positive, behave as for ROUND_DOWN; if negative, behave as for ROUND_UP. Note that this rounding mode never increases the calculated value.

ROUND_HALF_UP

public static final int ROUND_HALF_UP
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is >= .5; otherwise, behaves as for ROUND_DOWN. Note that this is the rounding mode that most of us were taught in grade school.

ROUND_HALF_DOWN

public static final int ROUND_HALF_DOWN
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. Behaves as for ROUND_UP if the discarded fraction is > .5; otherwise, behaves as for ROUND_DOWN.

ROUND_HALF_EVEN

public static final 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. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it's even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.

ROUND_UNNECESSARY

public static final int ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown.
Constructor Detail

BigDecimal

public BigDecimal(String val)
Translates the String representation of a BigDecmal into a BigDecimal. The String representation consists of an optional minus sign followed by a sequence of zero or more decimal digits, optionally followed by a fraction. The fraction consists of of a decimal point followed by zero or more decimal digits. The string must contain at least one digit in the integer or fractional part. The scale of the resulting BigDecimal will be the number of digits to the right of the decimal point in the string, or 0 if the string contains no decimal point. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).
Parameters:
val - String representation of BigDecimal.
Throws:
NumberFormatException - val is not a valid representation of a BigDecimal.
See Also:
Character.digit(char, int)

BigDecimal

public BigDecimal(double val)
Translates a double into a BigDecimal. The scale of the BigDecimal is the smallest value such that (10scale * val) is an integer.

Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.

The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.

Parameters:
val - double value to be converted to BigDecimal.
Throws:
NumberFormatException - val is equal to Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, or Double.NaN.

BigDecimal

public BigDecimal(BigInteger val)
Translates a BigInteger into a BigDecimal. The scale of the BigDecimal is zero.
Parameters:
val - BigInteger value to be converted to BigDecimal.

BigDecimal

public BigDecimal(BigInteger unscaledVal,
                  int scale)
Translates a BigInteger unscaled value and an int scale into a BigDecimal. The value of the BigDecimal is (unscaledVal/10scale).
Parameters:
unscaledVal - unscaled value of the BigDecimal.
scale - scale of the BigDecimal.
Throws:
NumberFormatException - scale is negative
Method Detail

valueOf

public static BigDecimal valueOf(long unscaledVal,
                                 int scale)
Translates a long unscaled value and an int scale into a BigDecimal. This "static factory method" is provided in preference to a (long, int) constructor because it allows for reuse of frequently used BigDecimals.
Parameters:
unscaledVal - unscaled value of the BigDecimal.
scale - scale of the BigDecimal.
Returns:
a BigDecimal whose value is (unscaledVal/10scale).

valueOf

public static BigDecimal valueOf(long val)
Translates a long value into a BigDecimal with a scale of zero. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigDecimals.
Parameters:
val - value of the BigDecimal.
Returns:
a BigDecimal whose value is val.

add

public BigDecimal add(BigDecimal val)
Returns a BigDecimal whose value is (this + val), and whose scale is max(this.scale(), val.scale()).
Parameters:
val - value to be added to this BigDecimal.
Returns:
this + val

subtract

public BigDecimal subtract(BigDecimal val)
Returns a BigDecimal whose value is (this - val), and whose scale is max(this.scale(), val.scale()).
Parameters:
val - value to be subtracted from this BigDecimal.
Returns:
this - val

multiply

public BigDecimal multiply(BigDecimal val)
Returns a BigDecimal whose value is (this * val), and whose scale is (this.scale() + val.scale()).
Parameters:
val - value to be multiplied by this BigDecimal.
Returns:
this * val

divide

public BigDecimal divide(BigDecimal val,
                         int scale,
                         int roundingMode)
Returns a BigDecimal whose value is (this / val), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied.
Parameters:
val - value by which this BigDecimal is to be divided.
scale - scale of the BigDecimal quotient to be returned.
roundingMode - rounding mode to apply.
Returns:
this / val
Throws:
ArithmeticException - val is zero, scale is negative, or roundingMode==ROUND_UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly.
IllegalArgumentException - roundingMode does not represent a valid rounding mode.
See Also:
ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY

divide

public BigDecimal divide(BigDecimal val,
                         int roundingMode)
Returns a BigDecimal whose value is (this / val), and whose scale is this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.
Parameters:
val - value by which this BigDecimal is to be divided.
roundingMode - rounding mode to apply.
Returns:
this / val
Throws:
ArithmeticException - val==0, or roundingMode==ROUND_UNNECESSARY and this.scale() is insufficient to represent the result of the division exactly.
IllegalArgumentException - roundingMode does not represent a valid rounding mode.
See Also:
ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY

abs

public BigDecimal abs()
Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().
Returns:
abs(this)

negate

public BigDecimal negate()
Returns a BigDecimal whose value is (-this), and whose scale is this.scale().
Returns:
-this

signum

public int signum()
Returns the signum function of this BigDecimal.
Returns:
-1, 0 or 1 as the value of this BigDecimal is negative, zero or positive.

scale

public int scale()
Returns the scale of this BigDecimal. (The scale is the number of digits to the right of the decimal point.)
Returns:
the scale of this BigDecimal.

unscaledValue

public BigInteger unscaledValue()
Returns a BigInteger whose value is the unscaled value of this BigDecimal. (Computes (this * 10this.scale()).)
Returns:
the unscaled value of this BigDecimal.
Since:
JDK1.2

setScale

public BigDecimal setScale(int scale,
                           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. If the scale is reduced by the operation, the unscaled value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division.
Parameters:
scale - scale of the BigDecimal value to be returned.
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.
Throws:
ArithmeticException - scale is negative, or roundingMode==ROUND_UNNECESSARY and the specified scaling operation would require rounding.
IllegalArgumentException - roundingMode does not represent a valid rounding mode.
See Also:
ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY

setScale

public BigDecimal setScale(int scale)
Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's. Throws an ArithmeticException if this is not possible. 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 loss of precision.

Note that this call returns the same result as the two argument version of setScale, but saves the caller the trouble of specifying a rounding mode in cases where it is irrelevant.

Parameters:
scale - scale of the BigDecimal value to be returned.
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.
Throws:
ArithmeticException - scale is negative, or the specified scaling operation would require rounding.
See Also:
setScale(int, int)

movePointLeft

public BigDecimal movePointLeft(int n)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left. If n is non-negative, the call merely adds n to the scale. If n is negative, the call is equivalent to movePointRight(-n). (The BigDecimal returned by this call has value (this * 10-n) and scale max(this.scale()+n, 0).)
Parameters:
n - number of places to move the decimal point to the left.
Returns:
a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.

movePointRight

public BigDecimal movePointRight(int n)
Moves the decimal point the specified number of places to the right. If this BigDecimal's scale is >= n, the call merely subtracts n from the scale; otherwise, it sets the scale to zero, and multiplies the integer value by 10(n - this.scale). If n is negative, the call is equivalent to movePointLeft(-n). (The BigDecimal returned by this call has value (this * 10n) and scale max(this.scale()-n, 0).)
Parameters:
n - number of places to move the decimal point to the right.
Returns:
a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.

compareTo

public int compareTo(BigDecimal val)
Compares this BigDecimal with the specified BigDecimal. Two BigDecimals that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
Parameters:
val - BigDecimal to which this BigDecimal is to be compared.
Returns:
-1, 0 or 1 as this BigDecimal is numerically less than, equal to, or greater than val.

compareTo

public int compareTo(Object o)
Compares this BigDecimal with the specified Object. If the Object is a BigDecimal, this method behaves like compareTo(BigDecimal). Otherwise, it throws a ClassCastException (as BigDecimals are comparable only to other BigDecimals).
Specified by:
compareTo in interface Comparable
Parameters:
o - Object to which this BigDecimal is to be compared.
Returns:
a negative number, zero, or a positive number as this BigDecimal is numerically less than, equal to, or greater than o, which must be a BigDecimal.
Throws:
ClassCastException - o is not a BigDecimal.
Since:
JDK1.2
See Also:
compareTo(java.math.BigDecimal), Comparable

equals

public boolean equals(Object x)
Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimals equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).
Overrides:
equals in class Object
Parameters:
o - Object to which this BigDecimal is to be compared.
Returns:
true if and only if the specified Object is a BigDecimal whose value and scale are equal to this BigDecimal's.
See Also:
compareTo(java.math.BigDecimal)

min

public BigDecimal min(BigDecimal val)
Returns the minimum of this BigDecimal and val.
Parameters:
val - value with with the minimum is to be computed.
Returns:
the BigDecimal whose value is the lesser of this BigDecimal and val. If they are equal, as defined by the compareTo method, either may be returned.
See Also:
compareTo(java.math.BigDecimal)

max

public BigDecimal max(BigDecimal val)
Returns the maximum of this BigDecimal and val.
Parameters:
val - value with with the maximum is to be computed.
Returns:
the BigDecimal whose value is the greater of this BigDecimal and val. If they are equal, as defined by the compareTo method, either may be returned.
See Also:
compareTo(java.math.BigDecimal)

hashCode

public int hashCode()
Returns the hash code for this BigDecimal. Note that two BigDecimals that are numerically equal but differ in scale (like 2.0 and 2.00) will generally not have the same hash code.
Overrides:
hashCode in class Object
Returns:
hash code for this BigDecimal.

toString

public String toString()
Returns the string representation of this BigDecimal. The digit-to- character mapping provided by Character.forDigit is used. A leading minus sign is used to indicate sign, and the number of digits to the right of the decimal point is used to indicate scale. (This representation is compatible with the (String) constructor.)
Overrides:
toString in class Object
Returns:
String representation of this BigDecimal.
See Also:
Character.forDigit(int, int), BigDecimal(java.lang.String)

toBigInteger

public BigInteger toBigInteger()
Converts this BigDecimal to a BigInteger. Standard narrowing primitive conversion as defined in The Java Language Specification: any fractional part of this BigDecimal will be discarded.
Returns:
this BigDecimal converted to a BigInteger.

intValue

public int intValue()
Converts this BigDecimal to an int. Standard narrowing primitive conversion as defined in The Java Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 bits are returned.
Overrides:
intValue in class Number
Returns:
this BigDecimal converted to an int.

longValue

public long longValue()
Converts this BigDecimal to a long. Standard narrowing primitive conversion as defined in The Java Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in a long, only the low-order 64 bits are returned.
Overrides:
longValue in class Number
Returns:
this BigDecimal converted to an int.

floatValue

public float floatValue()
Converts this BigDecimal to a float. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if this BigDecimal has too great a magnitude to represent as a float, it will be converted to FLOAT.NEGATIVE_INFINITY or FLOAT.POSITIVE_INFINITY as appropriate.
Overrides:
floatValue in class Number
Returns:
this BigDecimal converted to a float.

doubleValue

public double doubleValue()
Converts this BigDecimal to a double. Similar to the double-to-float narrowing primitive conversion defined in The Java Language Specification: if this BigDecimal has too great a magnitude to represent as a double, it will be converted to DOUBLE.NEGATIVE_INFINITY or DOUBLE.POSITIVE_INFINITY as appropriate.
Overrides:
doubleValue in class Number
Returns:
this BigDecimal converted to a double.

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.