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.