JavaScript中计算复利
复利公式
复利使用以下公式计算-
CI = P*(1 + R/n) (nt) – P
这里,
P是本金。
R是年利率。
t是金钱被投资或借入的时间。
n是每单位t复利的次数,例如,如果每月复利一次,t以年为单位,则n的值为12。如果每季度复利一次,t以年为单位,则n的值将是4。
我们需要编写一个JavaScript函数,该函数接受本金,利率,时间和数字n并计算复利。
示例
让我们为该函数编写代码-
const principal = 2000;
const time = 5;
const rate = .08;
const n = 12;
const compoundInterest = (p, t, r, n) => {
const amount = p * (Math.pow((1 + (r / n)), (n * t)));
const interest = amount - p;
return interest;
};
console.log(compoundInterest(principal, time, rate, n));输出结果
控制台中的输出:-
979.6914166032097
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短