使用JavaScript计算复合兴趣的未来值

时间:2011-01-17 11:22:42

标签: javascript finance

我正在尝试编写一个脚本,用户在该脚本中插入月收入,并在30年后获得复合利息的未来值。就像现在一样,我已经为测试目的分配了一些值。

// Future Value
var investment = 800;
var annualRate = 2;
var monthlyRate = annualRate / 12 / 100;
var years = 30;
var months = years * 12;
var futureValue = 0;

for ( i = 1; i <= months; i++ ) {
    futureValue = futureValue + investment * Math.pow(1 + monthlyRate, months);
}

问题是,我实际上是从使用内置FV()公式的Excel电子表格构建这个,并且在交叉检查时,我的结果完全关闭......任何想法我做错了,因为我'我根本没有进入金融数学。提前谢谢。

6 个答案:

答案 0 :(得分:8)

Math.pow是不必要的,因为您逐月计算并递增futureValue。只需乘以1 + monthlyRate即可。您还希望在乘以之前将投资的当前值添加到新投资中:

for ( i = 1; i <= months; i++ ) {
   futureValue = (futureValue + investment) * (1 + monthlyRate);
}

或者,您也可以使用以下公式一次性计算:

futureValue = investment * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate;

答案 1 :(得分:2)

下面是计算复利的代码。

function calculate() {
  p = document.getElementById("p").value;
  n = document.getElementById("n").value; // no. of compoundings per year
  t = document.getElementById("t").value; // no. of years
  r = document.getElementById("r").value;
  result = document.getElementById("result");

  // The equation is A = p * [[1 + (r/n)] ^ nt]
  A = (p * Math.pow((1 + (r / (n * 100))), (n * t)));

  // toFixed is used for rounding the amount with two decimal places.
  result.innerHTML = "The total amount is " + A.toFixed(2);

  result.innerHTML += "<br> The interest is " + (A.toFixed(2) - p).toFixed(2);
}
div {
  display: table-row;
}

label,
input {
  display: table-cell;
}
<html>

<head>
  <title>Compound Interest Calculation using jquery</title>
</head>
<body>
  <h1>Compound Interest Calculation Using JQery</h1>
  <div> <label>Amount: </label> <input id="p"> </div>
  <div> <label>Rate (%): </label> <input id="r"> </div>
  <div> <label>No. of Years: </label> <input id="t"> </div>
  <div> <label>Compunding Times Per Year: </label> <input id="n" value="1"> </div>

  <button onclick="calculate()">Calculate</button>

  <p id="result"></p>
</body>

</html>

答案 2 :(得分:0)

function FVcalc(PresentAmount,InterestRate,NumberOfYears) {
    var timescompound = 1;
    var AnnualInterestRate = (InterestRate/100)/timescompound;
    var Years= NumberOfYears

    var Periods=timescompound*Years;
    var NumPayments=Periods;
    var Prin=PresentAmount;

    MonthPayment=Math.floor((Prin)*(Math.pow((1+AnnualInterestRate),(Periods)))*100)/100;
    FVFactor=(Math.pow((1+AnnualInterestRate),(Periods)))
    return MonthPayment
}

http://www.uic.edu/classes/actg/actg500/pfvatutor.htm

答案 3 :(得分:0)

这是我编写复利的代码

的方法
function call()
    {
        var A = Principle;
        var B = Interest;
        var C = Years;
        var D = 0;
        var E = A*B/100;
        D+=E;
        var f=E+A;
        document.write("0 year: Interest "+E+" Principal: "+f);
        document.write("<br />");
        for (var i=1; i<C; i++)
        {
            E=f*B/100;
            D+=E;
            f=E+f;
            document.write(i+"year: Interest "+E+" Principal:"+f);
            document.write("<br />");
        }

    return false;
    }

答案 4 :(得分:0)

我也发现了这种替代方法:

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

答案 5 :(得分:0)

这在任何情况下都适用,请注意period的基准是一年1次,例如每6个月period = 2,4个月period = 3等……

var init = 500000
var years = 30
var compound = 1
var interest = 3.99
var period= 2
var total = 0



function CInterest(){
    for(let i=0; i<years*12; i++){
     
        if(i%(12/period) === 0){
            compound*=(1+((interest/100)/period))
        }
    }  
    total= init*compound
    }


    CInterest()
    console.log("total: ",total)