Java Reference
In-Depth Information
Caution
Never using
float
or
double
to represent monetary values.
Javaprovidesasolutiontobothproblemsintheformofa
BigDecimal
class.This
immutable class (a
BigDecimal
instance cannot be modified) represents a signed
decimalnumber(suchas23.653)ofarbitrary
precision
(numberofdigits)withanasso-
ciated
scale
(an integer that specifies the number of digits after the decimal point).
BigDecimal
declares three convenience constants:
ONE
,
TEN
, and
ZERO
. Each
constant is the
BigDecimal
equivalent of 1, 10, and 0 with a zero scale.
Caution
BigDecimal
declares several
ROUND_
-prefixed constants. These con-
stantsarelargelyobsoleteandshouldbeavoided,alongwiththe
BigDecimal di-
vide(BigDecimal divisor, int scale, int roundingMode)
and
BigDecimal setScale(int newScale, int roundingMode)
methods,
which are still present so that dependent legacy code continues to compile.
BigDecimal
also declares a variety ofuseful constructors and methods. Afew of
these constructors and methods are described in
Table 4-8
.