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

有帮助吗?

解决方案

This is how you should calculate your monthly payment.

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

I suggest using NSDecimalNumbers too.

其他提示

I was a little bit bored so I put the monthly rate formula I got from the internet into code that uses NSDecimalNumber.

As you will see it's a big pita to use NSDecimalNumbers. But you are writing financial software, and your customers expect results that are correct.
And once again, I'm pointing you at Marcus Zarras article DON’T BE LAZY WITH NSDECIMALNUMBER (LIKE ME).

enjoy.

NSDecimalNumber *loan = [NSDecimalNumber decimalNumberWithString:@"25000"];
NSDecimalNumber *annualInterestRate = [NSDecimalNumber decimalNumberWithString:@"6"]; // 6%
annualInterestRate = [annualInterestRate decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"100"]]; // 0.06 i.e. 6%
NSInteger numberOfPayments = 36;

// (loan * (interest / 12)) / (1 - (1 + interest / 12) ^ -number)
//          ^^^^^^^^^^^^^ temp1
// ^^^^^^^^^^^^^^^^^^^^^^^^ temp2
//                                      ^^^^^^^^^^^^^ temp1
//                                 ^^^^^^^^^^^^^^^^^^^ temp4 
//                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temp5
//                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temp6
NSDecimalNumber *temp1 = [annualInterestRate decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"12"]];
NSDecimalNumber *temp2 = [temp1 decimalNumberByMultiplyingBy:loan];
NSDecimalNumber *temp4 = [[NSDecimalNumber one] decimalNumberByAdding:temp1];
NSDecimalNumber *temp5 = [[NSDecimalNumber one] decimalNumberByDividingBy:[temp4 decimalNumberByRaisingToPower:numberOfPayments]];
NSDecimalNumber *temp6 = [[NSDecimalNumber one] decimalNumberBySubtracting:temp5];

NSDecimalNumber *monthlyRate = [temp2 decimalNumberByDividingBy:temp6];
NSLog(@"Monthly rate: %@", monthlyRate);

hope it's correct though. I usually don't have to deal with NSDecimalNumbers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top