在Xcode的Foundation框架中,NSFormatter只是一个抽象类,我们平常用到的是它的两个子类:NSNumberFormatter、NSDateFormatter,本篇先介绍NSNumberFormatter,NSDateFormatter会在下一篇进行介绍.
使用NSNumberFormatter可以将NSNumber转化成自己想要的格式来进行输出,同时NSNumberFormatter也支持使用NSString 类型的对象转换为NSNumber类型的对象
NSNumberFormatter支持的枚举类型如下,具体对应什么类型我会在代码里进行解释
typedef CF_ENUM(CFIndex, CFNumberFormatterStyle) { // number format styles
kCFNumberFormatterNoStyle = 0,
kCFNumberFormatterDecimalStyle = 1,
kCFNumberFormatterCurrencyStyle = 2,
kCFNumberFormatterPercentStyle = 3,
kCFNumberFormatterScientificStyle = 4,
kCFNumberFormatterSpellOutStyle = 5,
kCFNumberFormatterOrdinalStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 6,
kCFNumberFormatterCurrencyISOCodeStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 8,
kCFNumberFormatterCurrencyPluralStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 9,
kCFNumberFormatterCurrencyAccountingStyle CF_ENUM_AVAILABLE(10_11, 9_0) = 10,
};
使用方法一般如下:
一.使用NSNumber转化成自己想要的格式来进行输出
1.先创建一个NSNumerFormatter对象
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
2.创建一个NSNumber对象
NSNumber *number = [NSNumber numberWithDouble:12345.655521];
3.设置NSNumberFomatter的枚举值类型,
formatter1.numberStyle = kCFNumberFormatterNoStyle; //去掉小数点 四舍五入保留整数 12346
4.调用NSNumberFormatter的实例方法将NSNumebr对象转换为字符串格式:
NSString *strOfFormatter =[formatter stringFromNumber:number];
5.将该字符串输出
NSLog(@"%@",strOfFormatter);
下面我写了一个例子,仅供大家参考:
NSNumberFormatter *formatter1 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter2 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter3 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter4 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter5 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter6 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter7 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter8 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter9 = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatter10 = [[NSNumberFormatter alloc] init];
//原值 12345.655521
formatter1.numberStyle = kCFNumberFormatterNoStyle; //去掉小数点 四舍五入保留整数 12346
formatter2.numberStyle = NSNumberFormatterDecimalStyle; //以国际化格式输出 保留三位小数,第四位小数四舍五入 12,345.66
formatter3.numberStyle = kCFNumberFormatterCurrencyStyle; //以货币通用格式输出 保留2位小数,第三位小数四舍五入,在前面添加dollor符号 $12,345.66
formatter4.numberStyle = kCFNumberFormatterPercentStyle; //以百分制形式输出 整个数字乘以保留2为小数,第三位小数四舍五入,然后乘以100,同时在最后加上百分号
formatter5.numberStyle = kCFNumberFormatterScientificStyle; //以科学计数法输出 1.2345655521E4
formatter6.numberStyle = kCFNumberFormatterSpellOutStyle; //原值英文表示,
// 正常来说应该输出 twelve thousand, three hundred and forty-five point six five five five two one
//实际输出 twelve thousand three hundred forty-five point six five five five two one
//少了一个and ,这个跟系统设置的区域语言有关系
//还有其他的四种返回格式
formatter7.numberStyle = kCFNumberFormatterOrdinalStyle; //输出 12,346th
formatter8.numberStyle = kCFNumberFormatterCurrencyISOCodeStyle;//输出 USD12,345.66
formatter9.numberStyle = kCFNumberFormatterCurrencyPluralStyle;//输出 12,345.66 US dollars
formatter10.numberStyle = kCFNumberFormatterCurrencyAccountingStyle;//输出 $12,345.66
NSNumber *number = [NSNumber numberWithDouble:12345.655521];
NSLog(@"kCFNumberFormatterNoStyle=%@", [formatter1 stringFromNumber:number]);
NSLog(@"NSNumberFormatterDecimalStyle=%@", [formatter2 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterCurrencyStyle=%@", [formatter3 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterPercentStyle=%@", [formatter4 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterScientificStyle=%@", [formatter5 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterSpellOutStyle=%@", [formatter6 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterOrdinalStyle=%@", [formatter7 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterCurrencyISOCodeStyle=%@", [formatter8 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterCurrencyPluralStyle=%@", [formatter9 stringFromNumber:number]);
NSLog(@"kCFNumberFormatterCurrencyAccountingStyle=%@", [formatter10 stringFromNumber:number]);
输出结果如下:
2016-01-30 18:43:22.169 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterNoStyle=12346
2016-01-30 18:43:22.170 Test_NFNumberFormatter[1210:29952] NSNumberFormatterDecimalStyle=12,345.656
2016-01-30 18:43:22.171 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterCurrencyStyle=$12,345.66
2016-01-30 18:43:22.171 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterPercentStyle=1,234,566%
2016-01-30 18:43:22.171 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterScientificStyle=1.2345655521E4
2016-01-30 18:43:22.172 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterSpellOutStyle=twelve thousand three hundred forty-five point six five five five two one
2016-01-30 18:43:22.172 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterOrdinalStyle=12,346th
2016-01-30 18:43:22.173 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterCurrencyISOCodeStyle=USD12,345.66
2016-01-30 18:43:22.173 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterCurrencyPluralStyle=12,345.66 US dollars
2016-01-30 18:43:22.173 Test_NFNumberFormatter[1210:29952] kCFNumberFormatterCurrencyAccountingStyle=$12,345.66
二.使用NSString 类型的对象转换为NSNumber类型的对象
因为转化方式大同小异,所以我就接着上面的代码写了:
/*
同时,NSNumberFomatter也支持从NSString类型的对象转为NSNumber类型的对象
现在拿formatter1和formatter2来举例
先拿到NSString格式的数字
*/
NSString *strOfFormatter1 = [formatter1 stringFromNumber:number];
NSString *strOfFormatter2 = [formatter2 stringFromNumber:number];
// 目前 strOfFormatter1 = 12346
// strOfFormatter2 = 12,345.656
//转化为NSNumber格式
NSNumber *number1 = [formatter1 numberFromString:strOfFormatter1];
NSNumber *number2 = [formatter2 numberFromString:strOfFormatter2];
//打印number
NSLog(@"number1=%@",number1);
NSLog(@"number2=%@",number2);
输出结果为:
2016-01-30 19:24:47.400 Test_NFNumberFormatter[1335:49384] number1=12346
2016-01-30 19:24:47.943 Test_NFNumberFormatter[1335:49384] number2=12345.656
总结:NSNumberFormatter可以让我们方便的在NSString和NSNumber之间进行转化,当我们需要转化成一些特殊的格式的时候,这无疑给我们提供了很大的便利.文中提到的有个别格式是讲数字以通用的描述文字输出,文中输出的是twelve thousand three hundred forty-five point six five five five two one,这个是跟我们设置的区域语言参数有关系的,如果我们设置的是China,输出的文字就将会是中文的.(有人说通过在Info.plist文件中的字段Localization native development region设置的,设置为China就好了)但是我发现设置完之后还是没有办法打印出来中文的格式,各位可以自己试试.