Java Reference
In-Depth Information
This is another example of how operator overloading could make Java applications
simpler to read and write. If Java had overloaded the math operators when applied
to
BigNumber
s, this syntax could be used:
customerBalanceYen = customerBalance * currencyValue;
instead of this syntax:
customerBalanceYen = customerBalance.multiply (currencyValue);
As it is, the math functions provided with the
BigNumber
classes are the only way to
add, subtract, multiply, and divide
BigNumber
s.
The
BigDecimal
constructors and math functions also support an optional
MathContext
parameter. This class allows you to establish a particular set of
Big-
Decimal
attributes (scale, rounding mode) and use that set each time you need a
new
BigDecimal
.
MathContext currencyScale = new MathContext (123, RoundingMode.HALF_DOWN);
BigDecimal currencyValue = new BigDecimal (123, currencyScale);
BigDecimal currencyRate = new BigDecimal (1.3, currencyScale);
BigDecimal convertedCurrencyValue =
currencyValue.multiply (currencyRate, currencyScale);
Table 8.4 shows the most commonly used constructors for
BigDecimal
s and
BigInteger
s.
TABLE 8.4 COMMON CONSTRUCTORS FOR BIGDECIMAL AND BIGINTEGER
Constructor
Description
COBOL Equivalent
BigDecimal
Converts a BigInteger into a
MOVE
INTEGER1
TO
(BigInteger val)
BigDecimal
PACKED-NUM1
BigDecimal
Converts a BigInteger into a
MOVE
INTEGER1
TO
(BigInteger
BigDecimal with the scale requested
PACKED-NUM1
unScaledVal, int scale)
BigDecimal
Converts a double into a BigDecimal
MOVE
DOUBLE-NUM1
TO
(double val)
PACKED-NUM1*
BigDecimal (String val)
Converts a String representation of
MOVE WITH CONVERSION
a number into a BigDecimal
STRING1
TO
PACKED-NUM1
continued