I am trying to calculate the monthly payment of a loan and it always comes out wrong.
The formula is as follows where i is interest
((1 + i)^months /
(1 + i)^months - 1)
* principal * i
Assuming that annual interest rate and principal is an invisible floating point, can you tell me what's wrong with my formula?
double calculatePaymentAmount(int annualInterestRate,
int loanSize,
int numberOfPayments;
{
double monthlyInterest = annualInterestRate / 1200.0;
return
(
pow(1 + monthlyInterest, numberOfPayments) /
(pow(1 + monthlyInterest, numberOfPayments) - 1)
)
* (loanSize / 100)
* monthlyInterest;
}
For example: an interest rate of 1.25 and a loan size of 250 for 12 months gives 22.27 instead of 20.97.
Thank you in advance.
Edit 1: Changed monthly interest to annualInterestRate / 1200
I found what was wrong. monthlyInterest = annualInterestRate / 1200.0 / 100