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?
Asked
Active
Viewed 864 times
1 Answers
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
-
That's OK, but if I got value without places after dot, I'd like to show "1,234,567.00". How can I do it? – Metaller Aug 14 '11 at 18:16
-
@Metalier - apologies - I've changed the example. – martin clayton Aug 14 '11 at 18:23