算法学习:中文数组和阿拉伯数组互相转换

package ArrayAlgo;

public class NumberToChineseUtil {


    public static void main(String[] args) {
        String result = numToChinese(12345);
        System.out.println(result);
        System.out.println(chineseToNum(result));
    }

    //阿拉伯数字转中文数字
    public static String numToChinese(int num) {
        String[] units = {"", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿", "万亿"};
        char[] numArray = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
        char[] val = String.valueOf(num).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            String m = val[i] + "";
            int n = Integer.parseInt(m);
            boolean isZero = n == 0;
            String unit = units[len - 1 - i];
            if (isZero) {
                if ('0' == val[i - 1]) {
                    continue;
                } else {
                    sb.append(numArray[n]);
                }
            } else {
                sb.append(numArray[n]);
                sb.append(unit);
            }
        }
        if (sb.toString().endsWith("零")) {
            return sb.substring(0, sb.toString().lastIndexOf("零"));
        }
        return sb.toString();
    }


    //中文数字转阿拉伯数字
    public static int chineseToNum(String num) {
        char[] unitArr = {'十', '百', '千', '万', '亿'};
        char[] numArr = {'一', '二', '三', '四', '五', '六', '七', '八', '九'};
        int result = 0;
        int temp = 1;
        int count = 0;
        for (int i = 0; i < num.length(); i++) {
            boolean unit = true;//判断是否是chArr
            char c = num.charAt(i);
            //判断是数字还是单位
            for (int j = 0; j < numArr.length; j++) {
                if (c == numArr[j]) {
                    if (0 != count) {
                        result += temp;
                        temp = 1;
                        count = 0;
                    }
                    temp = j + 1;
                    unit = false;
                    break;
                }
            }

            if (unit) {
                for (int j = 0; j < unitArr.length; j++) {
                    if (c == unitArr[j]) {
                        switch (j) {
                            case 0:
                                temp = temp * 10;
                                break;
                            case 1:
                                temp = temp * 100;
                                break;
                            case 2:
                                temp = temp * 1000;
                                break;
                            case 3:
                                temp = temp * 10000;
                                break;
                            case 4:
                                temp = temp * 100000000;
                                break;
                            default:
                                break;
                        }
                        count++;
                    }
                }
            }

            if (i == num.length() - 1) {
                result += temp;
            }

        }
        return result;
    }


}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值