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

https://www.d5.nz/read/sfdlq/text-part0000_split_029.html

	private char[] chnNum = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
    private String[] chnSectionUnit = {"", "万", "亿", "万亿"};
    private String[] chnNumUnit     = {"", "十", "百", "千"};

    public String numberToChinese(long num) {
        int secUnitPos = 0;
        boolean needZero = false;
        StringBuilder sb = new StringBuilder();

        while (num > 0) {
            int section = (int) (num % 10000);

            // 上一个section是否 > 0 and < 1000
            if (needZero) {
                sb.insert(0, chnNum[0]);
            }

            sb.insert(0, chnSectionUnit[secUnitPos]);

            sectionToChinese(section, sb);

            needZero = section > 0 && section < 1000;
            secUnitPos++;
            num = num / 10000;
        }
        return sb.toString();
    }

    private void sectionToChinese(int section, StringBuilder sb) {
        int numUnitPos = 0;
        boolean lastCharIsZero = true;

        while (section > 0) {
            int v = section % 10;

            if (v == 0) {
                // 注意连续的0,   0101与1001
                // section == 0 或者 (section != 0 and zero = false)
                if (section == 0 || !lastCharIsZero) {
                    lastCharIsZero = true;
                    sb.insert(0, chnNum[0]);
                }
            }
            else {
                lastCharIsZero = false;
                sb.insert(0, chnNumUnit[numUnitPos]);
                sb.insert(0, chnNum[v]);
            }
            numUnitPos++;
            section = section / 10;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值