前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 数字转汉字(不同语言环境)

iOS 数字转汉字(不同语言环境)

作者头像
清墨
发布2019-03-04 10:38:27
2K0
发布2019-03-04 10:38:27
举报
文章被收录于专栏:清墨_iOS分享

1.用NSNumberFormatter 关于NSNumberFormatter,请看这里https://www.jianshu.com/p/817029422a72 我们这里只说数字转汉字的实现: NSNumberFormatter的numberStyle如下:

代码语言:javascript
复制
/*
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是输出中文的,代码如下

代码语言:javascript
复制
NSNumberFormatter  *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
NSString *ch_string = [formatter stringFromNumber:[NSNumber numberWithInt:6]];

输出则是六,但在实际情况下,如果我们手机当前语言不是中文,输出则不一定是六,如英文时是six。若要要求输出一定是中文,则再加以下代码,指定输出语言(别的语言同理):

代码语言:javascript
复制
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans"];
formatter.locale = locale;

2.用算法实现

代码语言:javascript
复制
/**
 *  将阿拉伯数字转换为中文数字
 */
-(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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.02.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档