NSNumberFormatter
相信大家对 NSDateFormatter比较地熟悉,是对NSDate进行格式化。今天介绍下NSNumberFormatter对数字进行格式化。
- 默认输出
NSNumber *num = [NSNumber numberWithDouble:54312345.67833];
NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
NSLog(@"%@",[format stringFromNumber:num]); //54312346
-
@property NSNumberFormatterStyle numberStyle
根据NSNumberFormatterStyle的定义,我们看到
typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle,
};
我们可以针对每个项进行Log
NSNumber *num = [NSNumber numberWithDouble:54312345.67833];
NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
// NSLog(@"%@",[format stringFromNumber:num]); //54312346
NSArray *numberStyleStrings = @[@"NSNumberFormatterNoStyle",
@"NSNumberFormatterDecimalStyle",
@"NSNumberFormatterCurrencyStyle",
@"NSNumberFormatterPercentStyle",
@"NSNumberFormatterScientificStyle",
@"NSNumberFormatterSpellOutStyle",];
for (int i = 0; i < 6; i++) {
format.numberStyle = (NSNumberFormatterStyle)i;
NSLog(@"%@:%@",numberStyleStrings[i],[format stringFromNumber:num]);
}
输出为
2018-04-24 23:00:34.512714+0800 hasasasd[26665:1118710] NSNumberFormatterNoStyle:54312346
2018-04-24 23:00:34.513077+0800 hasasasd[26665:1118710] NSNumberFormatterDecimalStyle:54,312,345.678
2018-04-24 23:00:34.513559+0800 hasasasd[26665:1118710] NSNumberFormatterCurrencyStyle:$54,312,345.68
2018-04-24 23:00:34.513812+0800 hasasasd[26665:1118710] NSNumberFormatterPercentStyle:5,431,234,568%
2018-04-24 23:00:34.514041+0800 hasasasd[26665:1118710] NSNumberFormatterScientificStyle:5.431234567833E7
2018-04-24 23:00:34.514963+0800 hasasasd[26665:1118710] NSNumberFormatterSpellOutStyle:fifty-four million three hundred twelve thousand three hundred forty-five point six seven eight three three
@property (null_resettable, copy) NSLocale *locale;
format.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
for (int i = 0; i < 6; i++) {
format.numberStyle = (NSNumberFormatterStyle)i;
NSLog(@"%@:%@",numberStyleStrings[i],[format stringFromNumber:num]);
}
输出为
2018-04-24 23:05:11.274317+0800 hasasasd[26726:1122487] NSNumberFormatterNoStyle:54312346 //四舍五入
2018-04-24 23:05:11.274636+0800 hasasasd[26726:1122487] NSNumberFormatterDecimalStyle:54,312,345.678 //保留3位小数
2018-04-24 23:05:11.275293+0800 hasasasd[26726:1122487] NSNumberFormatterCurrencyStyle:¥54,312,345.68 //货币表示
2018-04-24 23:05:11.275538+0800 hasasasd[26726:1122487] NSNumberFormatterPercentStyle:5,431,234,568% //百分数
2018-04-24 23:05:11.275799+0800 hasasasd[26726:1122487] NSNumberFormatterScientificStyle:5.431234567833E7 //科学计数
2018-04-24 23:05:11.276783+0800 hasasasd[26726:1122487] NSNumberFormatterSpellOutStyle:五千四百三十一万二千三百四十五点六七八三三 //中文模式
-
@property (null_resettable, copy) NSString *decimalSeparator;
//更改小数模式下分隔符
format.decimalSeparator = @"h";//更改小数分隔符
format.numberStyle = NSNumberFormatterDecimalStyle;//切换到小数模式
NSLog(@"%@",[format stringFromNumber:num]); //54,312,345h678
NSLog(@"%@",format.decimalSeparator);//h
-
@property BOOL alwaysShowsDecimalSeparator;
//是否总是显示小数分隔符 -
@property (null_resettable, copy) NSString *currencyDecimalSeparator;
//更改货币模式下分隔符
format.currencyDecimalSeparator = @"m";
format.numberStyle = NSNumberFormatterCurrencyStyle;
NSLog(@"%@",[format stringFromNumber:num]);//¥54,312,345h68
NSLog(@"%@",format.currencyDecimalSeparator);//m