你的位置:首页 > 操作系统

[操作系统]iOS之数字的格式化


//通过NSNumberFormatter,同样可以设置NSNumber输出的格式。例如如下代码:NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];formatter.numberStyle = NSNumberFormatterDecimalStyle;NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]];NSLog(@"Formatted number string:%@",string);//输出结果为:[1223:403] Formatted number string:123,456,789//其中NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括:typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {  NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,  NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,  NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,  NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,  NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,  NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle};//各个枚举对应输出数字格式的效果如下:其中第三项和最后一项的输出会根据系统设置的语言区域的不同而不同。[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:一亿二千三百四十五万六千七百八十九