NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括:
enum {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle, //无格式
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle, //小数型
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle, //货币型
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle, //百分比型
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle, //科学计数型
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle //全拼
};
typedef NSUInteger NSNumberFormatterStyle;
各个枚举对应输出数字格式的效果如下:
[1243:403] Formatted number string:123456789
[1243:403] Formatted number string:123,456,789
[1243:403] Formatted number string:¥123,456,789.00
[1243:403] Formatted number string:-539,222,988%
[1243:403] Formatted number string:1.23456789E8
[1243:403] Formatted number string:一亿二千三百四十五万六千七百八十九
其中第三项和最后一项的输出会根据系统设置的语言区域的不同而不同。
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormattersetNumberStyle:NSNumberFormatterSpellOutStyle];
NSString *priceStr = [numFormatterstringFromNumber:product.introPrice];