Calculating future value with compound interest with JavaScript

后端 未结 6 1566
悲哀的现实
悲哀的现实 2021-02-04 19:39

I\'m trying to work on a script where the user inserts a monthly income and gets the future value with compound interest after 30 years. As it is now, I\'ve assigned some values

相关标签:
6条回答
  • 2021-02-04 20:01

    This is my way of writing code for compound interest

    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;
        }
    
    0 讨论(0)
  • 2021-02-04 20:07

    This will work for any case, note that period has a benchmark of 1 of a year so for instance every 6 months period = 2, 4 months period = 3 etc...

    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)
    
    0 讨论(0)
  • 2021-02-04 20:08

    The Math.pow is unnecessary, since you are calculating and incrementing futureValue month by month. Simply multiply by 1 + monthlyRate. You also want to add the current value of the investment to the new investment before multiplying:

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

    Alternatively, you can also calculate this in one go with the following formula:

    futureValue = investment * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate;
    
    0 讨论(0)
  • 2021-02-04 20:14
    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

    0 讨论(0)
  • 2021-02-04 20:18

    Below is the code to calculate compound interest.

    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 jQuery</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>

    0 讨论(0)
  • 2021-02-04 20:21

    Also I found with this alternative:

    function compoundInterest(principal, annual_rate, n_times, t_years) {
        return principal*(Math.pow(1 + annual_rate/n_times, n_times*t_years) - 1);
    }
    
    0 讨论(0)
提交回复
热议问题