Java中文大写数字转int型

暂时不支持万以上级别,后面会加上这个。道理很简单,但我搜了一下google,找到的代码没有几个能直接用,所以分享下,直接贴代码了:

    public static int chnNumToIntConverter(String chnNum){
        HashMap<String, Integer> numbers = new HashMap<String, Integer>();
        numbers.put(“零”, 0);
        numbers.put(“一”, 1);
        numbers.put(“二”, 2);
        numbers.put(“三”, 3);
        numbers.put(“四”, 4);
        numbers.put(“五”, 5);
        numbers.put(“六”, 6);
        numbers.put(“七”, 7);
        numbers.put(“八”, 8);
        numbers.put(“九”, 9);
        HashMap<String, Integer> units = new HashMap<String, Integer>();
        units.put(“十”, 10);
        units.put(“百”, 100);
        units.put(“千”, 1000);
       
        int result = 0;
       
        //补全“十三”这种类型前的“一”
        if(chnNum.startsWith(“十”)){
            chnNum = “一” + chnNum;
        }
       
        int length = chnNum.length();
        for(int i=0; i<length; i++){
            String num = chnNum.substring(i, i+1);
            if(! num.equals(“零”)){
                if(i+2 > length){
                    result += numbers.get(num);
                }else{
                    String unit = chnNum.substring(i+1, i+2);
                    result += numbers.get(num) * units.get(unit);
                }
                i++;
            }
        }
       
        return result;
    }

你可能对下面的文章感兴趣

  1. Java int型转换中文大写数字表示方法
  2. Eclipse JFace Binding/绑定 (1)

One thought on “Java中文大写数字转int型

  1. Pingback: 没有比人更高的山 » Java int型转换中文大写数字表示方法

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">