0

So far I have

double futurevalue = moneyin * (1+ interest) * year;
Aaron
  • 11,239
  • 18
  • 58
  • 73

2 Answers2

4

The Java is correct, the fomular plain wrong. Compund interest is calculated this way:

Kn = K0 * (1 + p/100)n

where n is the number of periods and p is the "interest" per period (annual, if you look at years, p=annual/12 and n=12 if you look at month, have an annual interest as input and want to calculate for a year)


public double compoundInterest(double start, double interest, int periods) {
   return start * Math.pow(1 + interest/100, periods);
}

(Note: interest is a percentage value, like 4.2 for 4.2%)

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

I assume it's the power part of the formula you are having trouble with (multiplying by the year isn't right). For simple compound interest with whole numbers of years you can use the Math.pow() function that's part of the Java SDK.

double futureValue = moneyIn * Math.pow(1 + interest, year)
Jon
  • 5,247
  • 6
  • 30
  • 38
  • It's worth considering that most modern financial institutions compound daily, and so the above formula isn't correct if this is supposed to simulate a typical bank's calculation for example. Daily compounding does get messy however, as you must consider leap years. – SplinterReality Apr 13 '11 at 06:38