iOS之阿拉伯数字转中文数字

https://www.jianshu.com/p/e910cf9d5a62

 

 

这里总共介绍两种方法:

方法一:

    double testNum = 3.7999;

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
/*
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,
 };

*/

    formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
//    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans"];
//    formatter.locale = locale;
    NSString *string = [formatter stringFromNumber:[NSNumber numberWithDouble: testNum]];
    NSLog(@"%@",string);
};

注意:必须手机系统语言为中文的前提下才可用,如果是非中文环境需要加上中文输出限制.为准确输出,建议使用double类型

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans"];
formatter.locale = locale;
 
中文语言环境输出.png
 
英文语言环境输出.png

方法二 算法实现

/**
 *  将阿拉伯数字转换为中文数字
 */
-(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;
    }
}


作者:brilliance_Liu
链接:https://www.jianshu.com/p/e910cf9d5a62
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
posted @ 2019-08-15 20:06  sundaysios  阅读(494)  评论(0编辑  收藏  举报