阿拉伯数字转汉字数字

阿拉伯数字转汉字数字

public class ChineseNumberGenerUtil  {
    
    public static String CastToChineseNumber(int intNum) {
        String num = String.valueOf(intNum);
        String CNum = num;// 设置将要返回的数字为等于输入的数字
        if (CheckNum(CNum)) {
            switch (num.length()) {
            case 0:// 如果沒有输入数字
                CNum = "N/A"; // 未知
                break;
            case 1:// 如果是1位的数字
                CNum = GetCNum(num);
                break;
            case 2:// 2位的数字
                if (num.substring(1).equals("0")) {// 处理 10,20,3090这类数字
                    if (num.substring(0, 1).equals("1")) {
                        CNum = "十";
                    } else {
                        CNum = GetCNum(num.substring(0, 1)) + "十";
                    }
                } else if (num.substring(0, 1).equals("1")) {// 处理11转换为 十一 而不是一十一
                    CNum = "十" + GetCNum(num.substring(1));
                } else {
                    CNum = GetCNum(num.substring(0, 1)) + "十" + GetCNum(num.substring(1));
                }
                break;
            case 3:// 如果是3位的數字
                CNum = GetCNum(num.substring(0, 1)) + "百" + GetCNum(num.substring(1, 2)) + "十" + GetCNum(num.substring(2));
                break;
            default:
                break;
            }
        }        
        return CNum;
    }

    public static boolean CheckNum(String num) {
        List<String> al = new ArrayList<String>();
        String[] ss = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        al = Arrays.asList(ss);
        char[] nums = num.toCharArray();
        boolean isNum = false;
        for (char n : nums) {            
            if (al.contains(String.valueOf(n))) {
                isNum = true;
            } else {
                isNum = false;
                break;
            }
        }
        return isNum;
    }

    /**
     *     获取对应的汉字
     * @param num输入的数字
     * @return 返回的汉字
     */
    public static String GetCNum(String num) {
        String Cnum = "";
        switch (Integer.parseInt(num)) {
        case 0:
            Cnum = "零";
            break;
        case 1:
            Cnum = "一";
            break;
        case 2:
            Cnum = "二";
            break;
        case 3:
            Cnum = "三";
            break;
        case 4:
            Cnum = "四";
            break;
        case 5:
            Cnum = "五";
            break;
        case 6:
            Cnum = "六";
            break;
        case 7:
            Cnum = "七";
            break;
        case 8:
            Cnum = "八";
            break;
        case 9:
            Cnum = "九";
            break;
        }
        return Cnum;
    }

 

posted @ 2020-11-19 20:10  怡安  阅读(406)  评论(0编辑  收藏  举报