美文网首页
阿拉伯数字转换为中文数字

阿拉伯数字转换为中文数字

作者: 随心_追梦 | 来源:发表于2017-06-14 14:56 被阅读0次
    /** 
     *  将阿拉伯数字转换为中文数字 
     */  
    +(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;  
        }  
    }  
    

    相关文章

      网友评论

          本文标题:阿拉伯数字转换为中文数字

          本文链接:https://www.haomeiwen.com/subject/evnsqxtx.html