Performing the Interest Calculations with BigDecimal

Lines 21–22 perform the interest calculation using BigDecimal methods multiply, add and pow. The expression in line 22 evaluates as follows:

1. First, the expression rate.add(BigDecimal.ONE) adds 1 to the rate to produce a BigDecimal containing 1.05—this is equivalent to 1.0 + rate in line 19 of Fig. 5.6. The BigDecimal constant ONE represents the value 1. Class BigDecimal also provides the commonly used constants ZERO (0) and TEN (10).

2. Next, BigDecimal method pow is called on the preceding result to raise 1.05 to the power year—this is equivalent to passing 1.0 + rate and year to method Math.pow in line 19 of Fig. 5.6.

3. Finally, we call BigDecimal method multiply on the principal object, ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.