1 //通过NSNumberFormatter,同样可以设置NSNumber输出的格式。例如如下代码: 2 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 3 formatter.numberStyle = NSNumberFormatterDecimalStyle; 4 NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]]; 5 NSLog(@"Formatted number string:%@",string); 6 //输出结果为:[1223:403] Formatted number string:123,456,789 7 8 //其中NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括: 9 typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) { 10 NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle, 11 NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle, 12 NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle, 13 NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle, 14 NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle, 15 NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle 16 }; 17 //各个枚举对应输出数字格式的效果如下:其中第三项和最后一项的输出会根据系统设置的语言区域的不同而不同。 18 [1243:403] Formatted number string:123456789 19 [1243:403] Formatted number string:123,456,789 20 [1243:403] Formatted number string:¥123,456,789.00 21 [1243:403] Formatted number string:-539,222,988% 22 [1243:403] Formatted number string:1.23456789E8 23 [1243:403] Formatted number string:一亿二千三百四十五万六千七百八十九