我有一个代表浮点数的字符串,例如2400.0
. 我想将其格式化为数字(2,400.0
),我需要在数字符号后保留零。
NSString* theString = @"2400.0";
// I convert the string to a float
float f = [theString floatValue];
// here I lose the digit information :( and it ends up with 2400 instead of 2400.0
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setUsesSignificantDigits:YES];
[formatter setMinimumFractionDigits:1];
[formatter setMaximumFractionDigits:2];
[formatter setLocale:[NSLocale currentLocale]];
NSString *result = [formatter stringFromNumber:@(f)];
我NSLog
需要的result
时候2,400
2,400.0
如何获得正确的字符串?