The java.math Package
Contents:
java.math.BigDecimal (JDK 1.1)
java.math.BigInteger (JDK 1.1)
The java.math package, new in Java 1.1, contains classes for arbitrary-precision integer and floating-point arithmetic. Arbitrary-length integers are required for cryptography, and arbitrary-precision floating-point values are useful for financial applications that need to be careful about rounding errors. The class hierarchy of this extremely small package is shown in Figure 27.1.
Figure 27.1: The java.math package
java.math.BigDecimal (JDK 1.1)
This subclass of java.lang.Number represents a floating-point number of arbitrary size and precision. Its methods duplicate the functionality of the standard Java arithmetic operators. The compareTo() method compares the value of two BigDecimal objects and returns -1, 0, or 1 to indicate the result of the comparison.
BigDecimal objects are represented as an integer of arbitrary size and an integer scale that specifies the number of decimal places in the value. When working with BigDecimal values, you can explicitly specify the amount of precision (the number of decimal places) you are interested in. Also, whenever a BigDecimal method may discard precision (in a division operation, for example), you are required to specify what sort of rounding should be performed on the digit to the left of the discarded digit or digits. The eight constants defined by this class specify the available rounding modes. Because the BigDecimal class provides arbitrary precision and gives you explicit control over rounding and the number of decimal places you are interested in, it can be useful when dealing with quantities that represent money, or in other circumstances where the tolerance for rounding errors is low.
public classBigDecimalextends Number { //Public ConstructorspublicBigDecimal(Stringval) throws NumberFormatException; publicBigDecimal(doubleval) throws NumberFormatException; publicBigDecimal(BigIntegerval); publicBigDecimal(BigIntegerval, intscale) throws NumberFormatException; //Constantspublic static final intROUND_CEILING; public static final intROUND_DOWN; public static final intROUND_FLOOR; public static final intROUND_HALF_DOWN; public static final intROUND_HALF_EVEN; public static final intROUND_HALF_UP; public static final intROUND_UNNECESSARY; public static final intROUND_UP; //Class Methodspublic static BigDecimalvalueOf(longval, intscale) throws NumberFormatException; public static BigDecimalvalueOf(longval); //Public Instance Methodspublic BigDecimalabs(); public BigDecimaladd(BigDecimalval); public intcompareTo(BigDecimalval); public BigDecimaldivide(BigDecimalval, intscale, introundingMode) throws ArithmeticException, IllegalArgumentException; public BigDecimaldivide(BigDecimalval, introundingMode) throws ArithmeticException, IllegalArgumentException; public doubledoubleValue(); //Defines Numberpublic booleanequals(Objectx); //Overrides Objectpublic floatfloatValue(); //Defines Numberpublic inthashCode(); //Overrides Objectpublic intintValue(); //Defines Numberpublic longlongValue(); //Defines Numberpublic BigDecimalmax(BigDecimalval); public BigDecimalmin(BigDecimalval); public BigDecimalmovePointLeft(intn); public BigDecimalmovePointRight(intn); public BigDecimalmultiply(BigDecimalval); public BigDecimalnegate(); public intscale(); public BigDecimalsetScale(intscale, introundingMode) throws ArithmeticException, IllegalArgumentException; public BigDecimalsetScale(intscale) throws ArithmeticException, IllegalArgumentException; public intsignum(); public BigDecimalsubtract(BigDecimalval); public BigIntegertoBigInteger(); public StringtoString(); //Overrides Object}
Hierarchy:
Object->Number(Serializable)->BigDecimal