Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating future value with compound interest with JavaScript

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 for testing purposes.

// 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);
}

Problem is, I'm actually building this from an Excel spreadsheet that's using the built-in FV() formula and when cross checking, my results are totally off... Any idea what I'm doing wrong since I'm not into finance math at all. Thanks in advance.

like image 666
Alex Berg Avatar asked Jan 17 '11 11:01

Alex Berg


People also ask

How does Javascript calculate future value?

The future value is simply principal times (1 + interest rate) ^ time, or in this case, roughly 100*(1.01)^120. af.

How do you calculate the future value of compound interest?

Calculator Use The future value formula is FV=PV(1+i)n, where the present value PV increases for each period into the future by a factor of 1 + i. The future value calculator uses multiple variables in the FV calculation: The present value sum. Number of time periods, typically years.

What JS compound interest?

In simple terms, compound interest is interest you earn on interest. With a savings account that earns compound interest, you earn interest on the initial principal plus on the interest that accumulates over time.

How do you calculate future value with compound interest in Excel?

A more efficient way of calculating compound interest in Excel is applying the general interest formula: FV = PV(1+r)n, where FV is future value, PV is present value, r is the interest rate per period, and n is the number of compounding periods.


2 Answers

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;
like image 159
David Tang Avatar answered Sep 30 '22 00:09

David Tang


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>
like image 25
Yasin Sunni Avatar answered Sep 30 '22 01:09

Yasin Sunni