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 红包祝福语简短10字
18 周六早晨祝福语简短