For some reason calculating APR is very difficult. A lot of people ask, but not a lot of great answers. I'm working on a project, where I need to calculate APR. I have already calculated what the loan is, how much the user has to pay each month, and I have the amount of months. I found a formula online, but it doesn't make a lot of sense.
function calculateAPR(loan, repayments, months) {
var p = 1;
var tmp = 1;
var a = p;
var b = 0;
while (Math.abs(tmp) > 0.0001) {
p = (a - b) / 2 + b;
tmp = (loan / repayments) - (1 - Math.pow(1 + p, -months)) / p;
if (tmp > 0) {
a = p;
} else {
b = p;
}
}
var apr = Math.pow((1 + p), 12) - 1;
return apr;
}
Math.pow()'s second argument is what you want to power by an amount. Basically 1+p^12 on the third last line. Math.abs() returns the absolute value (-10 becomes 10). If I input this:
loan = 80000
repayments = 7366
months = 12
I get an APR of 20.56%. That seems correct according to the site I got the formula from, but when I input the numbers into other websites, I get no where near the same result.
Can someone explain how this formula works? Why is there a loop? Is it because of compound interest? Is there a better formula? I'm looking for any kind of help, because I can't seem to find answers that solve this programmably. Thanks.
r = 0.01andn = 180? – MortenMoulder Feb 07 '17 at 09:39