zoukankan      html  css  js  c++  java
  • 阿拉伯数字转汉字数字

    阿拉伯数字转汉字数字

    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;
        }
  • 相关阅读:
    模型绑定功能
    接口返回的内容
    跨平台的ASP.NET Core简介
    NLog如何打印日志(.Net5)
    注意力创造价值;
    Restful接口的介绍
    电脑设置双屏显示(windows)
    Linq多集合连接
    调试时才执行的代码
    mvc4 路由匹配测试
  • 原文地址:https://www.cnblogs.com/xiangpeng/p/14007650.html
Copyright © 2011-2022 走看看