Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating APR

Tags:

c#

math

financial

So, as the title says I need to calculate the APR for a loan. I have the following:

apr = (rate * ((Math.Pow((1 + rate), duration))) /
      ((Math.Pow((1 + rate), duration)) - 1)) -
      (installment / (loanamount - extracost));

But its not returning the correct value. I also tried another version of that equation with even worse results:

apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);

The calculations are all wrong. I tried adjusting some parenthesis and checking the order of operations. Any help would be greatly appreciated!

like image 687
Lord Relix Avatar asked Mar 05 '26 12:03

Lord Relix


2 Answers

Maybe it sounds like cheating, but I just used the existing VB financial library .NET has. Its for annuities so you have to make the payment a negative number.

Term is full number of payments(i.e. 15 year mortgage has 180 payments so 180) Payment is payment per (so monthly payment amt... x -1) Starting Amount = original loan amount with no fees

  double apr = Microsoft.VisualBasic.Financial.Rate(term,-payment,originalLoanAmt)

Then just multiply by the answer by the annual # of payments (if monthly then 12) or if you want the % expressed as a number like 3.25%, then multiply by 100 as well.

**note you have to add a reference to Microsoft.VisualBasic assembly.

like image 124
j snooze Avatar answered Mar 07 '26 07:03

j snooze


You're calculating the payment amount given duration as the number of payments. That's not the APR. Here's what I'm seeing:

double apr, loanamount = 3000d, extracost = 7000d, rate = 0.05;
int duration = 1;
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
Console.WriteLine(apr);
duration = 2;
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
Console.WriteLine(apr);
duration = 3;
apr = ((loanamount + extracost) * rate * Math.Pow((1 + rate), duration)) / (Math.Pow((1 + rate),duration) - 1);
Console.WriteLine(apr);

where the output is

10500
5378.0487804878
3672.08564631245

Only the first answer is the same as what you might expect from A=Pe^(rt) or similar calculations of the amount from the principal.
The actual calculation you need depends on both the legal jurisdiction and the type of credit/loan though.

like image 32
AutoIncrement Avatar answered Mar 07 '26 09:03

AutoIncrement



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!