开发者

How would I write the formula for compound interest in an expression in Java?

S开发者_JAVA百科o far I have

double futurevalue = moneyin * (1+ interest) * year;


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%)


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜