bubibu bubibu - 22 days ago 5
Objective-C Question

Objective c Format float with comma and 2 decimal places

I am trying to format my float into something like this "3,343,000.55" and turn it into a string.

Using the code below, I get something like this "3,343,000.00".

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numberFormatter.usesGroupingSeparator = YES;
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
NSNumber *myNum = [NSNumber numberWithInt:myFloat];
NSString *myFor = [numberFormatter stringFromNumber:myNum];

Answer

You need to just replace this below line

//it will change your value as double
NSNumber *myNum = [NSNumber numberWithDouble:3343000.55];

just change from numberWithInt to numberWithDouble