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.
The future value is simply principal times (1 + interest rate) ^ time, or in this case, roughly 100*(1.01)^120. af.
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.
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.
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.
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;
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With