问题
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?
回答1:
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];
来源:https://stackoverflow.com/questions/7058600/how-to-format-double-value-into-string-with-2-decimal-places-after-dot-and-with