comma separated thousand NSString stringWithFormat

Leo picture Leo · Dec 5, 2009 · Viewed 12.3k times · Source

Is it possible to display 1000.99 as 1,000.99 using

[NSString stringWithFormat:@"£%0.2f", 1000.99]

Please guide me if I am not on the right track to achieve this?

Answer

Chris Gummer picture Chris Gummer · Dec 6, 2009

Using NSNumberFormatter:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat: 1000.99]];
NSLog(@"%@", numberAsString);