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 妈妈生日祝福语简短励志