iOS 阿拉伯数字转化为中文数字

(NSString*)translation:(NSString *)arebic


{   NSString *str = arebic;

    NSArray *arabic_numerals =@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];

    NSArray *chinese_numerals =@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];

    NSArray *digits =@[@"",@"",@"",@"",@"",@"",@"",@"",@"亿",@"",@"",@"",@""];

    NSDictionary *dictionary = [NSDictionarydictionaryWithObjects:chinese_numerals forKeys:arabic_numerals];

    

    NSMutableArray *sums = [NSMutableArray array];

    for (int i = 0; i < str.length; i ++) {

        NSString *substr = [str substringWithRange:NSMakeRange(i, 1)];

        NSString *a = [dictionary objectForKey:substr];

        NSString *b = digits[str.length -i-1];

        NSString *sum = [a stringByAppendingString:b];

        if ([a isEqualToString:chinese_numerals[9]])

        {

            if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]])

            {

                sum = b;

                if ([[sums lastObject]isEqualToString:chinese_numerals[9]])

                {

                    [sums removeLastObject];

                }

            }else

            {

                sum = chinese_numerals[9];

            }

            

            if ([[sums lastObjectisEqualToString:sum])

            {

                continue;

            }

        }

        

        [sums addObject:sum];

    }

    

    NSString *sumStr = [sums  componentsJoinedByString:@""];

    NSString *chinese = [sumStr substringToIndex:sumStr.length-1];

    NSLog(@"%@ to %@",str,chinese);

    return chinese;

}

你可能感兴趣的:(iOS 阿拉伯数字转化为中文数字)