2

I have a double value in Objective-C and I need to place its value into label. How should I format it into string with 2 decimal places after dot and with separators of thousands?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Metaller
  • 504
  • 3
  • 10

1 Answers1

3

Use NSNumberFormatter, for example:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"#,##0.00" ];

NSString *string = [formatter stringFromNumber:[ NSNumber numberWithDouble:1234567.8901 ] ];
// string is now "1,234,567.89"
[formatter release];
martin clayton
  • 76,436
  • 32
  • 213
  • 198