Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 348
نمايش فراداده

BigDecimaljava.math

Java 1.1serializable comparable

This subclass of java.lang.Number represents a floating-point number of arbitrary size and precision. Because it uses a decimal rather than binary floating-point representation, it is not subject to the rounding errors that the float and double types are. This makes BigDecimal well-suited to financial and similar applications.

BigDecimal provides add( ) , subtract( ), multiply( ), and divide( ) methods to support basic arithmetic. In Java 5.0, this class has been expanded to define many more methods, including pow( ) for exponentiation. Many of the new methods use a MathContext to specify the desired precision of the result and the RoundingMode to be used to achieve that precision.

BigDecimal extends Number and implements the Comparable interface. The compareTo( ) method compares the value of two BigDecimal objects and returns -1, 0, or 1 to indicate the result of the comparison. Use this method in place of the <, <=, >, and >= operators that you'd use with float and double values.

A BigDecimal object is 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 precision (i.e., the number of decimal places) you are interested in. Also, whenever a BigDecimal method can discard precision (e.g., in a division operation), 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. In Java 5.0, however, the preferred way to specify a rounding mode is with the enumerated type RoundingMode.

Figure 11-1. java.math.BigDecimal

public class

BigDecimal extends Number implements Comparable<BigDecimal> { // Public Constructors public

BigDecimal (BigInteger

val );

5.0 public

BigDecimal (int

val );

5.0 public

BigDecimal (long

val ); public

BigDecimal (String

val );

5.0 public

BigDecimal (char[ ]

in ); public

BigDecimal (double

val );

5.0 public

BigDecimal (long

val , MathContext

mc );

5.0 public

BigDecimal (int

val , MathContext

mc );

5.0 public

BigDecimal (double

val , MathContext

mc );

5.0 public

BigDecimal (String

val , MathContext

mc );

5.0 public

BigDecimal (char[ ]

in , MathContext

mc ); public

BigDecimal (BigInteger

unscaledVal , int

scale );

5.0 public

BigDecimal (BigInteger

val , MathContext

mc );

5.0 public

BigDecimal (BigInteger

unscaledVal , int

scale , MathContext

mc );

5.0 public

BigDecimal (char[ ]

in , int

offset , int

len );

5.0 public

BigDecimal (char[ ]

in , int

offset , int

len , MathContext

mc ); // Public Constants

5.0 public static final BigDecimal

ONE ; public static final int

ROUND_CEILING ; =2 public static final int

ROUND_DOWN ; =1 public static final int

ROUND_FLOOR ; =3 public static final int

ROUND_HALF_DOWN ; =5 public static final int

ROUND_HALF_EVEN ; =6 public static final int

ROUND_HALF_UP ; =4 public static final int

ROUND_UNNECESSARY ; =7 public static final int

ROUND_UP ; =0

5.0 public static final BigDecimal

TEN ;

5.0 public static final BigDecimal

ZERO ; // Public Class Methods public static BigDecimal

valueOf (long

val );

5.0 public static BigDecimal

valueOf (double

val ); public static BigDecimal

valueOf (long

unscaledVal , int

scale ); // Public Instance Methods public BigDecimal

abs ( );

5.0 public BigDecimal

abs (MathContext

mc ); public BigDecimal

add (BigDecimal

augend );

5.0 public BigDecimal

add (BigDecimal

augend , MathContext

mc );

5.0 public byte

byteValueExact ( ); public int

compareTo (BigDecimal

val ); Implements:Comparable

5.0 public BigDecimal

divide (BigDecimal

divisor ); public BigDecimal

divide (BigDecimal

divisor , int

roundingMode );

5.0 public BigDecimal

divide (BigDecimal

divisor , RoundingMode

roundingMode );

5.0 public BigDecimal

divide (BigDecimal

divisor , MathContext

mc ); public BigDecimal

divide (BigDecimal

divisor , int

scale , int

roundingMode );

5.0 public BigDecimal

divide (BigDecimal

divisor , int

scale , RoundingMode

roundingMode );

5.0 public BigDecimal[ ]

divideAndRemainder (BigDecimal

divisor );

5.0 public BigDecimal[ ]

divideAndRemainder (BigDecimal

divisor , MathContext

mc );

5.0 public BigDecimal

divideToIntegralValue (BigDecimal

divisor );

5.0 public BigDecimal

divideToIntegralValue (BigDecimal

divisor , MathContext

mc );

5.0 public int

intValueExact ( );

5.0 public long

longValueExact ( ); public BigDecimal

max (BigDecimal

val ); public BigDecimal

min (BigDecimal

val ); public BigDecimal

movePointLeft (int

n ); public BigDecimal

movePointRight (int

n ); public BigDecimal

multiply (BigDecimal

multiplicand );

5.0 public BigDecimal

multiply (BigDecimal

multiplicand , MathContext

mc ); public BigDecimal

negate ( );

5.0 public BigDecimal

negate (MathContext

mc );

5.0 public BigDecimal

plus ( );

5.0 public BigDecimal

plus (MathContext

mc );

5.0 public BigDecimal

pow (int

n );

5.0 public BigDecimal

pow (int

n , MathContext

mc );

5.0 public int

precision ( );

5.0 public BigDecimal

remainder (BigDecimal

divisor );

5.0 public BigDecimal

remainder (BigDecimal

divisor , MathContext

mc );

5.0 public BigDecimal

round (MathContext

mc ); public int

scale ( );

5.0 public BigDecimal

scaleByPowerOfTen (int

n ); public BigDecimal

setScale (int

newScale ); public BigDecimal

setScale (int

newScale , int

roundingMode );

5.0 public BigDecimal

setScale (int

newScale , RoundingMode

roundingMode );

5.0 public short

shortValueExact ( ); public int

signum ( );

5.0 public BigDecimal

stripTrailingZeros ( ); public BigDecimal

subtract (BigDecimal

subtrahend );

5.0 public BigDecimal

subtract (BigDecimal

subtrahend , MathContext

mc ); public BigInteger

toBigInteger ( );

5.0 public BigInteger

toBigIntegerExact ( );

5.0 public String

toEngineeringString ( );

5.0 public String

toPlainString ( );

5.0 public BigDecimal

ulp ( );

1.2 public BigInteger

unscaledValue ( ); // Methods Implementing Comparable public int

compareTo (BigDecimal

val ); // Public Methods Overriding Number public double

doubleValue ( ); public float

floatValue ( ); public int

intValue ( ); public long

longValue ( ); // Public Methods Overriding Object public boolean

equals (Object

x ); public int

hashCode ( ); public String

toString ( ); }

Passed To

javax.xml.datatype.DatatypeFactory.{newDuration( ), newXMLGregorianCalendar( ), newXMLGregorianCalendarTime( )}, javax.xml.datatype.Duration.multiply( ), javax.xml.datatype.XMLGregorianCalendar.{setFractionalSecond( ), setTime( )}

Returned By

java.util.Scanner.nextBigDecimal( ), javax.xml.datatype.XMLGregorianCalendar.getFractionalSecond( )