Reputation: 523
I am trying to use Javascript to get the monthly payment needed to reach a certain amount of money in a savings account given the annual interest rate, savings starting amount, final amount, and the amount of time to let it grow. Here is an example:
PV=1000
FV=10000
Nper = 5 * 12 = 60
Rate = 1% /12 = 0.0083%
Somehow the answer is $145.51
but every formula I try gives a different result. On excel it is used like this to get this answer: PMT(0.083%,60,1000,-10000)
, and I tried the following:
var pv = 1000;
var fv = -10000;
var i = 0.01 / 12;
var n = 60;
function payment() {
return (pv - fv) * (i) / (1 - (Math.pow(1 + i, -n)));
}
This did not give the desired answer. This gives me 188.03
and not 145.51
. Any idea why?? Is this not the correct equation? Thanks!
Upvotes: 1
Views: 683
Reputation: 99
Here is the correct equation for PMT. I've added a quick integration for verification. Source : https://gist.github.com/maarten00/23400873d51bf2ec4eeb
const pv = 1000;
const fv = -10000;
const i = 0.01 / 12;
const n = 60;
function pmt(rate_per_period, number_of_payments, present_value, future_value, type){
future_value = typeof future_value !== 'undefined' ? future_value : 0;
type = typeof type !== 'undefined' ? type : 0;
if(rate_per_period != 0.0){
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if(number_of_payments != 0.0){
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
document.getElementById("showPayment").innerHTML = pmt(i, n, pv, fv);
<div class="showPayment" id="showPayment">
</div>
Upvotes: 2