计算 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));
Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.
输出
控制台中的输出:-
979.6914166032097
广告