1.用NSNumberFormatter 关于NSNumberFormatter,请看这里https://www.jianshu.com/p/817029422a72 我们这里只说数字转汉字的实现: NSNumberFormatter的numberStyle如下:
/*
double testNum = 3.7999;
typedef CF_ENUM(CFIndex, CFNumberFormatterRoundingMode) {
kCFNumberFormatterRoundCeiling = 0, //四舍五入,直接输出4
kCFNumberFormatterRoundFloor = 1 , //保留小数输出3.8
kCFNumberFormatterRoundDown = 2, //加上了人民币标志,原值输出¥3.8
kCFNumberFormatterRoundUp = 3, //本身数值乘以100后用百分号表示,输出380%
kCFNumberFormatterRoundHalfEven = 4,//输出3.799999999E0
kCFNumberFormatterRoundHalfDown = 5,//原值的中文表示,输出三点七九九九。。。。
kCFNumberFormatterRoundHalfUp = 6//原值中文序数表示,输出第四
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,
};
*/
从上可以看出kCFNumberFormatterRoundHalfDown是输出中文的,代码如下
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
NSString *ch_string = [formatter stringFromNumber:[NSNumber numberWithInt:6]];
输出则是六,但在实际情况下,如果我们手机当前语言不是中文,输出则不一定是六,如英文时是six。若要要求输出一定是中文,则再加以下代码,指定输出语言(别的语言同理):
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans"];
formatter.locale = locale;
2.用算法实现
/**
* 将阿拉伯数字转换为中文数字
*/
-(NSString *)translationArabicNum:(NSInteger)arabicNum
{
NSString *arabicNumStr = [NSString stringWithFormat:@"%ld",(long)arabicNum];
NSArray *arabicNumeralsArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
NSArray *chineseNumeralsArray = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];
NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chineseNumeralsArray forKeys:arabicNumeralsArray];
if (arabicNum < 20 && arabicNum > 9) {
if (arabicNum == 10) {
return @"十";
}else{
NSString *subStr1 = [arabicNumStr substringWithRange:NSMakeRange(1, 1)];
NSString *a1 = [dictionary objectForKey:subStr1];
NSString *chinese1 = [NSString stringWithFormat:@"十%@",a1];
return chinese1;
}
}else{
NSMutableArray *sums = [NSMutableArray array];
for (int i = 0; i < arabicNumStr.length; i ++)
{
NSString *substr = [arabicNumStr substringWithRange:NSMakeRange(i, 1)];
NSString *a = [dictionary objectForKey:substr];
NSString *b = digits[arabicNumStr.length -i-1];
NSString *sum = [a stringByAppendingString:b];
if ([a isEqualToString:chineseNumeralsArray[9]])
{
if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])
{
sum = b;
if ([[sums lastObject] isEqualToString:chineseNumeralsArray[9]])
{
[sums removeLastObject];
}
}else
{
sum = chineseNumeralsArray[9];
}
if ([[sums lastObject] isEqualToString:sum])
{
continue;
}
}
[sums addObject:sum];
}
NSString *sumStr = [sums componentsJoinedByString:@""];
NSString *chinese = [sumStr substringToIndex:sumStr.length-1];
return chinese;
}
}