用 JavaScript 计算复利
javascript libraryweb developmentfront end technologyobject oriented programming更新于 2025/12/4 22:36:17
复利公式
复利使用以下公式计算 −
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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

