Java Reference
In-Depth Information
Consider that the number
5.6677221133445533229988776666554433221133
has a scale of 40 digits, which is a very small fraction of the format's maxi-
mum range. This means that for most practical purposes this range can be
considered of unlimited precision.
In addition to its high-precision, the BigDecimal class gives its users
control over rounding behavior, by providing eight rounding modes. As
discussed in Chapter 21, rounding control provides some interesting pro-
gramming possibilities, including the use of boundary arithmetic.
BigDecimal constructors
The BigDecimal class contains the following constructors:
BigDecimal(BigInteger val)
Translates a BigInteger into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale)
TranslatesaBigIntegerunscaledvalueandanintscaleintoaBigDecimal.
BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates a string into a BigDecimal.
Using the BigDecimal constructors
The first three constructors are used to convert from other numeric for-
matsintotheBigDecimalformat.Forexample,toconvertavalueindouble
precision format you can code:
double aDbl = 1.23;
BigDecimal bigNum = new BigDecimal(aDbl);
At this point the variable bigNum contains the value of the closest dou-
ble precision representation to the number 1.23. Since the value 1.23 can-
not be exactly represented as a binary floating-point number, the value
stored in bigNum is:
1.229999999999999982236431605997495353221893310546875
In order make sure of the value stored in a BigDecimal variable you
must use the constructor that takes a string operand. For example:
Search WWH ::




Custom Search