Final

java.math
Class BigDecimal

java.lang.Object
  extended byjava.lang.Number
      extended byjava.math.BigDecimal
All Implemented Interfaces:
Comparable, Serializable

public class BigDecimal
extends Number
implements Comparable

BigDecimal objects represent an arbitrary precisioned decimal Number. They contain values that cannot be changed. Thus, most operations on the BigDecimal object yield new instances of BigDecimal.

BigDecimal is respresented by an unscaled BigInteger value and an integer representing the scale of the object. The scale of the BigDecimal is the number of digits after the decimal point. Eg. 1.234 would have a scale of 3 and an unscaled value of 1234. Therefore, decimal representation of a BigDecimal is BigIntegerValue/10^scale.

See Also:
BigInteger, Serialized Form

Field Summary
static int ROUND_CEILING
           
static int ROUND_DOWN
           
static int ROUND_FLOOR
           
static int ROUND_HALF_DOWN
           
static int ROUND_HALF_EVEN
           
static int ROUND_HALF_UP
           
static int ROUND_UNNECESSARY
           
static int ROUND_UP
          Rounding mode Constants
 
Constructor Summary
BigDecimal(BigInteger bval)
          Constructs a BigDecimal with unscaled value initialized as bval and scale as 0.
BigDecimal(BigInteger bval, int sc)
          Constructs a BigDecimal with unscaled value initialized as bval and scale as scale from the argument.
BigDecimal(double bval)
          Constructs a BigDecimal with a double value as an arugment.
BigDecimal(String val)
          Constructs a BigDecimal from the strong which can only contan digits of 0-9, a decimal point and a negative sign.
 
Method Summary
 BigDecimal abs()
          Answers the absolute value of this BigDecimal.
 BigDecimal add(BigDecimal bval)
          Answers the sum of the receiver and argument.
 int compareTo(BigDecimal bval)
          Compares the receiver BigDecimal and argument BigDecimal e.x 1.00 & 1.0 will return 0 in compareTo.
 int compareTo(Object o)
          Compares an receiver to the argument Object.
 BigDecimal divide(BigDecimal bval, int roundingMode)
          Answers the result of (this / val).
 BigDecimal divide(BigDecimal bval, int bscale, int roundingMode)
          Answers the result of (this / val) and whose scale is specified.
 double doubleValue()
          Converts this BigDecimal to a double.
 boolean equals(Object obj)
          Compares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.
 float floatValue()
          Converts this BigDecimal to a float.If magnitude of the BigDecimal value is larger than what can be represented by a float, either Infinity or -Infinity is returned.
 int hashCode()
          Answers an integer hash code for the receiver.
 int intValue()
          Converts this BigDecimal to an int.
 long longValue()
          Converts this BigDecimal to a long.
 BigDecimal max(BigDecimal bval)
          Answers the max value between the receiver and this BigDecimal.
 BigDecimal min(BigDecimal bval)
          Answers the min value between the receiver and argument.
 BigDecimal movePointLeft(int n)
          Moves the decimal point of this BigDecimal n places to the left.
 BigDecimal movePointRight(int n)
          Moves the decimal point of this BigDecimal n places to the right.
 BigDecimal multiply(BigDecimal bval)
          Answers the multiplication result of the receiver and argument.
 BigDecimal negate()
          Negates this BigDecimal value.
 int scale()
          Returns the scale of this BigDecimal.
 BigDecimal setScale(int newScale)
          Sets the scale of this BigDecimal.
 BigDecimal setScale(int newScale, int roundingMode)
          Sets the scale of this BigDecimal.
 int signum()
          Answers the signum function of this instance.
 BigDecimal subtract(BigDecimal bval)
          Answers the subtract result of the receiver and argument.
 BigInteger toBigInteger()
          Converts this to a BigInteger.
 String toString()
          Answers a string containing a concise, human-readable description of the receiver.
 BigInteger unscaledValue()
          Returns an unscaled value of this BigDecimal.
static BigDecimal valueOf(long bval)
          Translate long value into a BigDecimal with scale of zero.
static BigDecimal valueOf(long bval, int scale)
          Translate long unscaled value into a BigDecimal specified by the scale.
 
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 Constants

See Also:
Constant Field Values

ROUND_DOWN

public static final int ROUND_DOWN
See Also:
Constant Field Values

ROUND_CEILING

public static final int ROUND_CEILING
See Also:
Constant Field Values

ROUND_FLOOR

public static final int ROUND_FLOOR
See Also:
Constant Field Values

ROUND_HALF_UP

public static final int ROUND_HALF_UP
See Also:
Constant Field Values

ROUND_HALF_DOWN

public static final int ROUND_HALF_DOWN
See Also:
Constant Field Values

ROUND_HALF_EVEN

public static final int ROUND_HALF_EVEN
See Also:
Constant Field Values

ROUND_UNNECESSARY

public static final int ROUND_UNNECESSARY
See Also:
Constant Field Values
Constructor Detail

BigDecimal

public BigDecimal(BigInteger bval)
Constructs a BigDecimal with unscaled value initialized as bval and scale as 0.


BigDecimal

public BigDecimal(BigInteger bval,
                  int sc)
Constructs a BigDecimal with unscaled value initialized as bval and scale as scale from the argument.


BigDecimal

public BigDecimal(double bval)
Constructs a BigDecimal with a double value as an arugment.

Throws:
NumberFormatException - If the is Infinity, Negative Infinity or NaN.

BigDecimal

public BigDecimal(String val)
Constructs a BigDecimal from the strong which can only contan digits of 0-9, a decimal point and a negative sign.

Throws:
NumberFormatException - If the argument contained characters other than digits.
Method Detail

abs

public BigDecimal abs()
Answers the absolute value of this BigDecimal.

Returns:
BigDecimal absolute value of the receiver.

add

public BigDecimal add(BigDecimal bval)
Answers the sum of the receiver and argument.

Returns:
BigDecimal The sum of adding two BigDecimal.

compareTo

public int compareTo(Object o)
Compares an receiver to the argument Object.

Specified by:
compareTo in interface Comparable
Parameters:
o - Object an object to compare the receiver to
Returns:
int 0 - equal; 1 - this > val; -1 - this < val
Throws:
ClassCastException - if the argument is not of type BigDecimal

compareTo

public int compareTo(BigDecimal bval)
Compares the receiver BigDecimal and argument BigDecimal e.x 1.00 & 1.0 will return 0 in compareTo.

Returns:
int 0 - equal; 1 - this > val; -1 - this < val.

divide

public BigDecimal divide(BigDecimal bval,
                         int roundingMode)
Answers the result of (this / val).

Returns:
BigDecimal result of this/val.

divide

public BigDecimal divide(BigDecimal bval,
                         int bscale,
                         int roundingMode)
Answers the result of (this / val) and whose scale is specified.

Returns:
BigDecimal result of this/val.
Throws:
ArithmeticException - division by zero.
IllegalArgumentException - roundingMode is not valid.

doubleValue

public double doubleValue()
Converts this BigDecimal to a double. If magnitude of the BigDecimal value is larger than what can be represented by a double, either Infinity or -Infinity is returned.

Specified by:
doubleValue in class Number
Returns:
double the value of the receiver.

equals

public boolean equals(Object obj)
Description copied from class: Object
Compares the argument to the receiver, and answers true if they represent the same object using a class specific comparison. The implementation in Object answers true only if the argument is the exact same object as the receiver (==).

Overrides:
equals in class Object
Parameters:
obj - Object the object to compare with this object.
Returns:
boolean true if the object is the same as this object false if it is different from this object.
See Also:
Object.hashCode()

floatValue

public float floatValue()
Converts this BigDecimal to a float.If magnitude of the BigDecimal value is larger than what can be represented by a float, either Infinity or -Infinity is returned.

Specified by:
floatValue in class Number
Returns:
float the value of the receiver.

hashCode

public int hashCode()
Answers an integer hash code for the receiver. Any two objects which answer true when passed to .equals must answer the same value for this method.

Overrides:
hashCode in class Object
Returns:
int the receiver's hash.
See Also:
equals(Object)

intValue

public int intValue()
Converts this BigDecimal to an int.

Specified by:
intValue in class Number
Returns:
int the value of the receiver.

longValue

public long longValue()
Converts this BigDecimal to a long.

Specified by:
longValue in class Number
Returns:
long long representation of the receiver.

max

public BigDecimal max(BigDecimal bval)
Answers the max value between the receiver and this BigDecimal.

Returns:
BigDecimal max BigDecimal.

min

public BigDecimal min(BigDecimal bval)
Answers the min value between the receiver and argument.

Returns:
BigDecimal max BigDecimal.

movePointLeft

public BigDecimal movePointLeft(int n)
Moves the decimal point of this BigDecimal n places to the left.

Returns:
BigDecimal new BigDecimal with decimal moved n places to the left.

movePointRight

public BigDecimal movePointRight(int n)
Moves the decimal point of this BigDecimal n places to the right.

Returns:
BigDecimal new BigDecimal with decimal moved n places to the right.

multiply

public BigDecimal multiply(BigDecimal bval)
Answers the multiplication result of the receiver and argument.

Returns:
BigDecimal result of multiplying two bigDecimals.

negate

public BigDecimal negate()
Negates this BigDecimal value.

Returns:
BigDecimal new BigDecimal with value negated.

scale

public int scale()
Returns the scale of this BigDecimal.

Returns:
int scale value.

setScale

public BigDecimal setScale(int newScale)
Sets the scale of this BigDecimal.

Returns:
BigDecimal a BigDecimal with the same value, but specified scale.

setScale

public BigDecimal setScale(int newScale,
                           int roundingMode)
Sets the scale of this BigDecimal. The unscaled value is determined by the rounding Mode

Returns:
BigDecimal a BigDecimal with the same value, but specified cale.
Throws:
ArithmeticException - rounding mode must be specified if lose of precision due to setting scale.
IllegalArgumentException - invalid rounding mode

signum

public int signum()
Answers the signum function of this instance.

Returns:
int -1, 0, or 1 if the receiver is negative, zero, or positive.

subtract

public BigDecimal subtract(BigDecimal bval)
Answers the subtract result of the receiver and argument.

Returns:
BigDecimal The result of adding two BigDecimal.

toBigInteger

public BigInteger toBigInteger()
Converts this to a BigInteger.

Returns:
BigInteger BigDecimal equivalent of bigInteger.

toString

public String toString()
Answers a string containing a concise, human-readable description of the receiver.

Overrides:
toString in class Object
Returns:
String a printable representation for the receiver.

unscaledValue

public BigInteger unscaledValue()
Returns an unscaled value of this BigDecimal.

Returns:
BigInteger The unscaled value.

valueOf

public static BigDecimal valueOf(long bval)
Translate long value into a BigDecimal with scale of zero.

Returns:
BigDecimal BigDecimal equivalence of a long value.

valueOf

public static BigDecimal valueOf(long bval,
                                 int scale)
Translate long unscaled value into a BigDecimal specified by the scale.

Returns:
BigDecimal BigDecimal equalvalence of a long value.
Throws:
NumberFormatException - the scale value is < 0;

Final

Licensed Materials - Property of IBM
(C) Copyright IBM Corp. 2006 All Rights Reserved.