1

Hey so I have test program for something I'm working on.

What does it do? - It basically minuses the monthly loan payments from the total amount payable and then inserts it into an array, it is working however I need it to be formatted to 2 decimal places and I have no idea how to do that, I've tried using a method I found which is

[NSString stringWithFormat: @"%.2lf", tempPaymentHolder]

But it doesn't seem to work, here is the code.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        //array that would hold the payments
        NSMutableArray * arrayWithPayments = [[NSMutableArray alloc]init];

        //total amount to be paid
        NSMutableString * totalPaymentString = [[NSMutableString alloc]initWithString:@"151.92"];

        //monthly payment
        NSMutableString * monthlyPaymentsString = [[NSMutableString alloc]initWithString:@"12.66"];

        //temp storage for passing values into array
        NSMutableString * tempPaymentStringHolder = [[NSMutableString alloc]init];
        tempPaymentStringHolder = [NSString stringWithFormat:@"%@",totalPaymentString];

        //doubles so I can calculate
        int numberOfMonths = 12;
        double monthlyPayments = [monthlyPaymentsString doubleValue];
        double totalPayment = [totalPaymentString doubleValue];
        double tempPaymentHolder = totalPayment;


        for (int i = 0; i <= numberOfMonths; i++) 
        {
            [arrayWithPayments addObject:tempPaymentStringHolder];
            tempPaymentHolder = totalPayment - monthlyPayments;
            totalPayment = tempPaymentHolder;
            tempPaymentStringHolder = [NSString stringWithFormat: @"%.2lf", tempPaymentHolder];
        }

        for (id obj in arrayWithPayments)
        {
            NSLog(@"%@",obj);
        }


    }
    return 0;
}

Would appreciate any help.

Thank you very much for your time.

user1214037
  • 153
  • 3
  • 12
  • 1
    What exactly is not working? I ran your code and it prints out strings formatted to two decimal places which seems to match your specifications. – FluffulousChimp Mar 21 '12 at 21:02
  • ah, thanks! it was working I was just looking at the wrong console output. sorry for the trouble – user1214037 Mar 21 '12 at 21:07
  • For currency, take a look at NSDecimalNumber's so that you don't have to worry about strange rounding errors of doubles. – lnafziger Mar 21 '12 at 21:07
  • Also, although you said this is just a test program - but converting back and forth between `NSString` and doubles doesn't make intuitive sense to me. See excellent comments about `NSDecimalNumber` in the answer to [this question](http://stackoverflow.com/questions/421463/should-i-use-nsdecimalnumber-to-deal-with-money) – FluffulousChimp Mar 21 '12 at 21:08

1 Answers1

4

Check out NSDecimalNumber:

NSMutableArray *arrayOfPayments = [[NSMutableArray alloc]init];

//total amount to be paid
NSDecimalNumber * totalPayment = [NSDecimalNumber decimalNumberWithString:@"151.92"];

//monthly payment
NSDecimalNumber * monthlyPayments = [NSDecimalNumber decimalNumberWithString:@"12.66"];

//temp storage for passing values into array
NSDecimalNumber* tempPaymentHolder = [totalPayment copy];

//variables so I can calculate
int numberOfMonths = 12;


for (int i = 0; i <= numberOfMonths; i++) 
{
    [arrayOfPayments addObject:tempPaymentHolder];
    tempPaymentHolder = [totalPayment decimalNumberBySubtracting:monthlyPayments];
    totalPayment = [tempPaymentHolder copy];
}

NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

for (NSDecimalNumber *obj in arrayOfPayments)
{
    NSLog(@"%@", [formatter stringFromNumber:obj]);
}

Output:

$151.92
$139.26
$126.60
$113.94
$101.28
$88.62
$75.96
$63.30
$50.64
$37.98
$25.32
$12.66
$0.00

Just be sure to remember to copy variables where you would normally assign them, as you are dealing with objects, not primitives here!

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • Yeah, I was looking at the wrong console output, sorry for the trouble and I will take a look at NSDecimalNumber - Thanks for the tip – user1214037 Mar 21 '12 at 21:09
  • +1, however, they aren't formatted to two decimals as requested. (An example of how to do this in addition to "upgrading" to NSDecimalNumber would answer the original question! ) – lnafziger Mar 21 '12 at 21:14
  • @Inafziger, please check the answer again, obviously you hadn't recieved my edits, but I added number formatter in there too. – Richard J. Ross III Mar 21 '12 at 21:15