简体   繁体   English

如何在Java表达式中编写复利公式?

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

到目前为止,我有

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

The Java is correct, the fomular plain wrong. Java是正确的,格式错误。 Compund interest is calculated this way: 利息是这样计算的:

K n = K 0 * (1 + p/100) n K n = K 0 *(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) 其中, n是周期数,p是每个周期的“利息”(每年,如果您查看年份,则p=annual/12n=12如果您查看月份,则将年度利息作为输入并想要计算一年)


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%) (注意:利息是一个百分比值,例如4.2代表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. 为了获得简单的整数年利息,可以使用Java SDK附带的Math.pow()函数。

double futureValue = moneyIn * Math.pow(1 + interest, year)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM