【问题标题】:How do I solve for continuous compounding interest in JavaScript?如何解决对 JavaScript 的持续复利?
【发布时间】:2020-05-27 05:24:24
【问题描述】:

我正在做一个项目,其中我们有三个变量,需要创建一个解决连续复利的函数:

三个变量分别是:

  1. 原理
  2. 年利率
  3. 年数

我目前的功能是:

function compoundInterest(balance, interest, years) {
  balance *= Math.pow(Math.E, (1 + interest) * years);
  return balance;
}

我需要遵循维基百科中的这个公式:

P(t)=P{0}e^{rt}

【问题讨论】:

  • 问题是什么?

标签: javascript math


【解决方案1】:

试试这个功能:

function compoundInterest(principal, annual_rate, n_times, t_years) {
    return principal*(Math.pow(1 + annual_rate/n_times, n_times*t_years) - 1);
}

【讨论】:

    【解决方案2】:

    您的公式中没有任何“1+”,因此您的 Javascript 代码中也不应该有任何“1+”。来自维基百科。

    • P 是原始本金和
    • r 是名义年利率
    • t 是应用利息的总时间长度(使用与 r 相同的时间单位表示,通常是年)

    因此,如果您想要连续复利函数 P(t)=P{0}e^{rt},您需要 Javascript:

    balance *= Math.pow(Math.E, interest * years);
    

    .

    【讨论】:

    • 非常感谢!我刚试过,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2022-06-17
    • 2019-10-31
    • 2021-04-04
    • 2020-07-17
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多