2

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

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user1214037
  • 153
  • 3
  • 12
  • 5
    first of all: don't use double if someone pays for that loan in real life. Use [NSDecimalNumber](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html) See the [question "Should I use NSDecimalNumber to deal with money?"](http://stackoverflow.com/questions/421463/should-i-use-nsdecimalnumber-to-deal-with-money) for the reasons – Matthias Bauch Mar 15 '12 at 14:15
  • thanks for the tip, i'll look into this. btw what is the difference? as in what would happen if someone pays for that loan in real life. – user1214037 Mar 15 '12 at 14:20
  • 1
    @user1214037: the difference is in accuracy. One should **NEVER** use a float variable in app that deals with finance. You always use a kind of integer for the smallest ammount of money in currency you're working with. You can only convert to float when displaying the final result. – Rok Jarc Mar 15 '12 at 16:45

2 Answers2

4

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.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

This is how you should calculate your monthly payment.

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

I suggest using NSDecimalNumbers too.

Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58
  • Thanks, I will look into how I can use NSDecimalNumber, I am quite new to objective c so I don't really know everything that is available, but I learn, bit by bit. :) – user1214037 Mar 15 '12 at 14:24
  • Glad I could help. If it's the right answer, you can mark it as the correct answer :) – Dominik Hadl Mar 15 '12 at 14:28
  • I've looked a little into NSDecimalNumber however, I cannot pass doubles into NSNumber (like in the code you provided) so do I need to go and convert all my doubles into NSDecimalNumber? is it also possible to perform calculations like in the code you provided? - Also it gives me an error: interface type cannot be statically allocated. – user1214037 Mar 15 '12 at 14:41
  • Yes, it would be better to convert them. You could do it like this (example) ------- NSDecimalNumber *interestRateDec = [NSDecimalNumber numberWithDouble:InterestRate]; ------- But you can try it out by changing the monthlyPayment to double. – Dominik Hadl Mar 15 '12 at 14:45
  • 2
    don't use `[NSDecimalNumber numberWithDouble:]` Don't turn your doubles into NSDecimalNumbers at all. Use `[NSDecimalNumber decimalNumberWithString:@"1.23"]` instead. Yes, NSDecimalNumbers are a PITA to use. But they provide accuracy. That's probably number 1 goal when you are dealing with financial software. Read this: http://www.cimgf.com/2008/04/23/cocoa-tutorial-dont-be-lazy-with-nsdecimalnumber-like-me/ – Matthias Bauch Mar 15 '12 at 15:37
  • and of course you have to use the NSDecimalNumber methods to calculate. Don't use something like `[NSDecimalNumber numberWithDouble:[decimalFoo doubleValue] + [decimalBar doubleValue]]` use `[decimalFoo decimalNumberByAdding:decimalBar]` instead. You see, big PITA. But accurate. – Matthias Bauch Mar 15 '12 at 15:45
  • This isn't how `NSDecimalNumber`s work; that code snippet is completely wrong. `NSDecimalNumber` is a class; the variable needs to be a pointer, and it needs to be initialized with a method call, not just arithmetic. – jscs Mar 15 '12 at 18:22
  • Thanks for the info - it is a pain in the ... but I guess I need to learn how to use them – user1214037 Mar 15 '12 at 19:44