iOS—阿拉伯数字转化为汉语数字

阿拉伯数字转化为汉语数字。近期项目中涉及到图书,章节顺序生成(1、2、3...)要在APP端处理章节.在这分享一个数字转换汉字的方法供大家一起学习参考。

+(NSString *)chaptersDealWithString:(NSString *)chapterStr
{   
NSString *str = chapter;
NSArray * chapter_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"];

NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys: chapter_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 lastObject] isEqualToString:sum])
        {
            continue;
        }
    }

    [sums addObject:sum];
}

NSString *sumStr = [sums  componentsJoinedByString:@""];
NSString *chinese = [sumStr substringToIndex:sumStr.length-1];
NSLog(@"%@",str);
NSLog(@"%@",chinese);
return chinese;

}

你可能感兴趣的:(iOS—阿拉伯数字转化为汉语数字)