User-Profile-Image
hankin
  • 5
  • 首页
  • 关于
  • [置顶] IT博文集
  • 数据仓库文档
  • 文章
    • 爱好(4)
    • 旅游(7)
    • 数据结构与算法(4)
    • 数据库技术(2)
    • 大数据处理(12)
    • 写作(10)
    • Java开发(1)
    • iOS开发(2)
    • ETL(2)
  • 页面
    • HiBuy
  • 友链
    • 字节集和
    • 伍捌贰柒
    • 嘉华丽
    • 井冈山大学梧桐树下
    • 张兴举个人网站
Help?

Please contact us on our email for need any support

Support
  • o
    首页   ›   计算机   ›   数据结构与算法   ›   正文
数据结构与算法

有用的算法-阿拉伯数字转中文读法代码

2020-12-08 11:02:06
1469  2 0

【有用的算法】

1、阿拉伯数字转中文读法代码

Tips:仅整数,万万亿往上有bug;提供Java、Objective-C版本代码

测试数据:

110000:一十一万
100000200:一亿零二百
1000000323:一十亿零三百二十三
9999100323340034:九千九百九十九万亿一千零三亿二千三百三十四万零三十四

Java版

public String numberConvert(double n) {
    if (n < 0) return "异常";
    double number = n;
    String[] numberChars = {"零","一","二","三","四","五","六","七","八","九"};
    String[] units = {"", "十", "百", "千", "万","十", "百", "千", "亿", "十",
            "百", "千", "万亿", "十", "百", "千", "万万亿", "十", "百", "千", "?"};
    StringBuilder result = new StringBuilder();
    int index = 0, isZero = 0; // index:数值单位下标递增,isZero:是否插入零,多个零只需读一个
    while (number >= 1) {
        if (index >= units.length) break;
        int pop = (int) (number % 10);
        number /= 10; // 从低位依次取出组成number的数字
        String unit = units[index ++];
        String readNum = numberChars[pop];
        if (pop > 0) result.insert(0, unit);
        else if (index % 4 == 1) result.insert(0, unit); // 避免零字后刚好跟单位
        if (pop != 0) {
            result.insert(0, readNum);
            isZero = 0;
        } else if (isZero == 0) { // readNum = 零且可插入零时,再避免零后刚好跟单位
            if (index % 4 != 1) result.insert(0, readNum);
            isZero = 1;
        }
    }
    if (n == 0) result.append(numberChars[0]);
    if (n / 10000 % 10000 < 1) {    // 解决亿级数字万级全零多了个万字问题
        int s = result.lastIndexOf("万");
        if (s > 0 && s + 1 < result.length())
            if (result.charAt(s + 1) != '零')
                result.replace(s, s + 1, "零");
            else result.delete(s, s + 1);
    }
    return result.toString();
}

Objective-C版

- (NSString *)numberConvert:(long)n {
    if (n < 0) return @"异常";
    long number = n;
    NSArray *numberChars = @[@"零", @"一", @"二", @"三", @"四", @"五", @"六", @"七", @"八", @"九"];
    NSArray *units = @[@"", @"十", @"百", @"千", @"万", @"十", @"百", @"千", @"亿", @"十", @"百",
                       @"千", @"万亿", @"十", @"百", @"千", @"万万亿", @"十", @"百", @"千", @"?"];
    NSMutableString *result = [[NSMutableString alloc] initWithCapacity:0];
    int index = 0, isZero = 0; // index:数值单位下标递增,isZero:是否插入零,多个零只需读一个
    while (number > 0) {
        if (index >= units.count) break;
        int pop = (int)(number % 10);
        number /= 10; // 从低位依次取出组成number的数字
        NSString *unit = units[index ++];
        NSString *readNum = numberChars[pop];
        if (pop > 0) [result insertString:unit atIndex:0];
        else if (index % 4 == 1) [result insertString:unit atIndex:0]; // 避免零字后刚好跟单位
        if (pop != 0) {
            [result insertString:readNum atIndex:0];
            isZero = 0;
        } else if (isZero == 0) { // readNum = 零且可插入零时,再避免零后刚好跟单位
            if (index % 4 != 1) [result insertString:readNum atIndex:0];
            isZero = 1;
        }
    }
    if (n == 0) [result appendString:numberChars[0]];
    if(n / 10000 % 10000 < 1) {    // 解决亿级数字万级全零多了个万字问题
        NSRange range = [result rangeOfString:units[4] options:NSBackwardsSearch];
        // PS:此处还应判断越界
        if ([[result substringWithRange:NSMakeRange(range.location + 1, 1)] isEqual:@"零"])
            [result deleteCharactersInRange:range];
        else [result replaceCharactersInRange:range withString:numberChars[0]];
    }
    return result;
}

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
评论 (2)

点击这里取消回复。

欢迎您 游客  

  • 有用 Mark一下

    2年前
    回复
    1. 欢迎光临~[Wow]

      2年前
      回复
 
无知的流浪清远,有知的穿行苦短
43文章 9评论 32点赞 42978浏览

最新文章
  • 力扣SQL题训练(二) 2021年2月17日
  • Spark多种方法实现二次排序 2021年2月1日
  • 力扣SQL题训练(一) 2021年1月30日
  • 有用的算法-阿拉伯数字转中文读法代码 2020年12月8日
  • JVM架构及GC垃圾回收机制及相应的参数调优 2020年11月16日
最新评论
  • Suit Negozi发表在《力扣SQL题训练(二)》
  • 刚键发表在《有用的算法-阿拉伯数字转中文读法代码》
  • 匿名发表在《有用的算法-阿拉伯数字转中文读法代码》
  • 刚键发表在《Pentaho Kettle 9.0.0.1 源码编译及探索(一)》
  • 匿名发表在《Pentaho Kettle 9.0.0.1 源码编译及探索(一)》
文章日历
2020年12月
一 二 三 四 五 六 日
 123456
78910111213
14151617181920
21222324252627
28293031  
  « 11月   1月 »
网站统计
  • 日志总数:43 篇
  • 评论数目:9 条
  • 建站日期:2015-04-12
  • 运行天数:2686 天
  • 标签总数:1 个
  • 页面总数:1 个
  • 分类总数:15 个
  • 最后更新:2021-3-7
Copyright © 2015-2022 黔ICP备2020011843号
  Theme by smarty_hankin
主页
页面
  • HiBuy
博主
刚键 管理员
Home :: Wandering how to make me stronger
43 文章 9 评论 42978 浏览
测试
测试
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

微信支付  支付宝