7th,September,2016
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = kCFNumberFormatterRoundHalfDown;
NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt: testNum]];
其中numberStyle:
typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle,
NSNumberFormatterOrdinalStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterOrdinalStyle,
NSNumberFormatterCurrencyISOCodeStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyISOCodeStyle,
NSNumberFormatterCurrencyPluralStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyPluralStyle,
NSNumberFormatterCurrencyAccountingStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyAccountingStyle,
};
demo
// 虽然苹果文档有给出实例,不过得再测测
float num = 38.27238;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterNoStyle;
NSString *str = [nf stringFromNumber:[NSNumber numberWithFloat:num]];
NSLog(@"show str: %@", str);
NSNumberFormatterNoStyle // 四舍五入到个位上 eg: 1234.56 –> 1235
NSNumberFormatterDecimalStyle // 四舍五入保留到当前小数位数少一位的位数上.? eg: 1234.5678 –> 1234.568 打印出来跟苹果的官方文档1234.5678 不一样
NSNumberFormatterCurrencyStyle // 根据当前locale(地区与语言),添加货币符号,比如英文$,中文则为¥.如果为美国地区,英文则会显示为US ¥
NSNumberFormatterPercentStyle // 转换为百分制,比如1.233–> 123%
NSNumberFormatterScientificStyle // 科学计数法? eg: 1234.5678–> 1.23456774902344E3跟苹果官方文档1.234568+003不一样
NSNumberFormatterSpellOutStyle // 根据locale(语言)会spell-out也就是将数字转换为相应语言的文字,eg: 1.23–> 一点二三 ; 30.23 ->thity point two three
NSNumberFormatterOrdinalStyle // 转换为相应的次序, eg: 12–> 12th(en_US_POSIX locale), 12–> 第12(中国地区中文)
NSNumberFormatterCurrencyISOCodeStyle // eg: 1234.5678–>CNY1234.57(中国地区中文);1234.5678–> USD 1234.57,测试了下中文美国地区,显示为USD,也就是取决于地区。
float num = 1234538.27238;
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterSpellOutStyle;
NSString *str = [nf stringFromNumber:[NSNumber numberWithFloat:num]];
当系统语言非中文时,根据上面的方法是无法转换成对应的汉子
// todo : rslt:一十万〇三百〇五, two:十万〇三百〇五 r: 今天已经不想再写这个了,留着之后改进吧
+ (NSString *)chineseWithArabString:(NSString *)arabStr {
NSArray *arab_numbers = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
NSArray *chinese_strs = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"〇"];
NSArray *digits = @[@"", @"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];
NSDictionary *tranDict = [NSDictionary dictionaryWithObjects:chinese_strs forKeys:arab_numbers];
NSString *chineseStr = @"";
NSMutableArray *sums = [NSMutableArray array];
for (int i = 0; i < arabStr.length; i++) {
NSString *subStr = [arabStr substringWithRange:NSMakeRange(i, 1)];
NSString *a = [tranDict objectForKey:subStr];
NSString *b = digits[arabStr.length - i - 1];
NSString *sum = [a stringByAppendingString:b];
if ([a isEqualToString:chinese_strs[9]]) {
if ([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]]) {
sum = b;
if ([[sums lastObject] isEqualToString:chinese_strs[9]]) {
[sums removeLastObject];
}
} else {
sum = chinese_strs[9];
}
if ([[sums lastObject] isEqualToString:sum]) {
continue;
}
}
[sums addObject:sum];
}
chineseStr = [sums componentsJoinedByString:@""];
return chineseStr;
}
iOS阿拉伯数字转化为中文数字