Formatting a (large) number "12345" to "12,345" Formatting a (large) number "12345" to "12,345" objective-c objective-c

Formatting a (large) number "12345" to "12,345"


Here is the answer.

  NSNumber* number = [NSNumber numberWithDouble:10000000];  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];  [numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle];  [numberFormatter setGroupingSeparator:@","];  NSString* commaString = [numberFormatter stringForObjectValue:number];  [numberFormatter release];  NSLog(@"%@ -> %@", number, commaString);


Try using an NSNumberFormatter.

This should allow you to handle this correctly on an iPhone. Make sure you use the 10.4+ style, though. From that page:

"iPhone OS: The v10.0 compatibility mode is not available on iPhone OS—only the 10.4 mode is available."


At least on Mac OS X, you can just use the "'" string formatter with printf(3).

$ man 3 printf

     `''          Decimal conversions (d, u, or i) or the integral portion                  of a floating point conversion (f or F) should be                  grouped and separated by thousands using the non-mone-                  tary separator returned by localeconv(3).

as in printf("%'6d",1000000);