Search code examples
iphoneobjective-ciosnsdecimalnumber

how to calculate loan repayment


Hey I am trying to calculate monthly repayments based on loan amount, interest rate and number of years.

I have come up with the following, however there seems to be a difference between my calculations and other loan calculators on the internet. my test data was $1000 loan amount, 5% interest over a period of 1 year. this gives me a monthly payment of 85.61 and a total payment as 1027.29

Here is the code that calculates this.

    double LoanAmount = la; //loan amount
    double InterestRate = ir; //interest rate
    double NumberOfYears = noy; //number of years

    double interestRateDecimal = InterestRate / (12 * 100);
    double months = NumberOfYears * 12;
    double rPower = pow(1+interestRateDecimal,months);

    monthlyPayments = LoanAmount * interestRateDecimal * rPower / (rPower - 1);
    totalPayments = monthlyPayments * months;
    yearlyPayments = monthlyPayments * 12;
    totalInterest = totalPayments - LoanAmount;

Is the formula I am using correct or are there errors?

Any help would be appreciated.

Thank you very much


Solution

  • This is how you should calculate your monthly payment.

    NSDecimalNumber monthlyPayment = LoanAmount * interestRateDecimal / (1 - (pow(1/(1 + interestRateDecimal), months)));
    

    I suggest using NSDecimalNumbers too.