2

It is often said (e.g. by Josh Bloch in "Effective Java") that we shouldn't use floating-point types, such as double, for financial calculations. How, then, can we calculate a monthly interest rate given an annual rate?

The formula is:

monthly rate = (1 + annual rate)^(1/12) - 1

But BigDecimal doesn't have a pow(BigDecimal) method, nor an nthRoot(int) method. So we can't callrate.pow(1.0/12)norrate.nthroot(12)` because those methods don't exist. If we do this:

monthlyRate = BigDecimal.valueOf(Math.pow(1 + rate.doubleValue(), 1.0/12) - 1)

then it defeats the purpose of using BigDecimal in order to avoid using double.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42

0 Answers0