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

    package com.test.utils.utils;  
      
    import java.util.HashMap;  
    import java.util.LinkedHashMap;  
    import java.util.Map;  
    import java.util.regex.Matcher;  
    import java.util.regex.Pattern;  
      
    public class ChineseNumber2ALB {  
          
        public static void main(String[] args) {  
            String text = "千啦啦啦,我有十块钱, 你有二十三块钱, 他有二元钱, 我是三百, 你是二百二, 还有五百六十七, 也许是十三, 那么九百零六, 哈哈一千零三十万零三百.";  
            System.out.println(bulidTextZHToALB(text));  
        }  
          
        public static String bulidTextZHToALB(String text) {  
            Pattern p = Pattern.compile(numRegex);  
            Matcher m = p.matcher(text);  
              
            while(m.find()) {  
                String numZH = m.group();  
                if(numZH.length() !=1 || numMap.containsKey(numZH) || zhTen.equals(numZH)) {  
                    String numALB = NumZHToALB(numZH);  
                    text = text.replaceFirst(numZH, numALB);  
                }  
            }  
              
            return text;  
        }  
          
        private static String NumZHToALB(String numZH) {  
            int numALB = 0;  
            int formIndex = 0;  
            for(String unitNum : unitNumMap.keySet()) {  
                int index = numZH.indexOf(unitNum);  
                if(index != -1 ) {  
                    numALB += NumZHToALB(numZH.substring(formIndex, index),  unitNumMap.get(unitNum));  
                    formIndex = index + 1;  
                }  
            }  
              
            numALB += NumZHToALB(numZH.substring(formIndex),  1);  
            return String.valueOf(numALB);  
        }  
          
        private static int NumZHToALB(String numZH, int unitNum) {  
            int length = numZH.length();  
            int numALB = 0;  
            if(length != 0) {  
                int fromIndex = 0;  
                for(String unit : unitMap.keySet()) {  
                    int index = numZH.indexOf(unit, fromIndex);  
                    if(index != -1) {  
                        fromIndex = index + unit.length();  
                        String prevChar = zhOne;  
                        if(index != 0 && numMap.containsKey(prevChar)) {  
                            prevChar = String.valueOf(numZH.charAt(index - 1));  
                        }   
                        numALB += numMap.get(prevChar) * unitMap.get(unit);  
                    }  
                }  
                  
                String lastChar = String.valueOf(numZH.charAt(length - 1));  
                if(numMap.containsKey(lastChar)) {  
                    String pChar = zhTen;  
                    if(length != 1) {  
                        pChar = String.valueOf(numZH.charAt(length - 2));  
                        if(zhZero.equals(pChar)) {  
                            pChar = zhTen;  
                        }  
                    }  
                    numALB += numMap.get(lastChar) * unitMap.get(pChar)/10;  
                }  
            }  
              
            return numALB * unitNum;  
        }  
          
        private static String encodeUnicode(String gbString) {     
            char[] utfBytes = gbString.toCharArray();     
            String unicodeBytes = "";     
            for (int i : utfBytes) {     
                String hexB = Integer.toHexString(i);     
                if (hexB.length() <= 2) {     
                    hexB = "00" + hexB;     
                }     
                unicodeBytes = unicodeBytes + "\u" + hexB;     
            }  
            return unicodeBytes;  
        }  
          
        private static final String zhZero = "零";  
        private static final String zhOne = "一";  
        private static final String zhTen = "十";  
          
        private static final Map<String, Integer> numMap = new HashMap<String, Integer>();  
        static {  
            numMap.put("零", 0);  
            numMap.put("一", 1);  
            numMap.put("二", 2);  
            numMap.put("三", 3);  
            numMap.put("四", 4);  
            numMap.put("五", 5);  
            numMap.put("六", 6);  
            numMap.put("七", 7);  
            numMap.put("八", 8);  
            numMap.put("九", 9);  
        }  
          
        private static final Map<String, Integer> unitNumMap = new LinkedHashMap<String, Integer>();  
        static {  
            unitNumMap.put("亿", 100000000);  
            unitNumMap.put("万", 10000);  
        }  
          
        private static final Map<String, Integer> unitMap = new LinkedHashMap<String, Integer>();  
        static {  
            unitMap.put("千", 1000);  
            unitMap.put("百", 100);  
            unitMap.put("十", 10);  
        }  
          
        private static String numRegex;  
        static {  
            numRegex = "[";  
            for(String s : numMap.keySet()) {  
                numRegex += encodeUnicode(s);  
            }  
            for(String s : unitMap.keySet()) {  
                numRegex += encodeUnicode(s);  
            }  
            for(String s : unitNumMap.keySet()) {  
                numRegex += encodeUnicode(s);  
            }  
            numRegex += "]+";  
        }  
    }  

    中文数字转换阿拉伯数字工具类

    您的资助是我最大的动力!
    金额随意,欢迎来赏!

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    欢迎大家关注我的个人博客 Lycos | 小站 !由于最近没时间处理,原域名http://www.liuyuchuan.com暂时停止使用

  • 相关阅读:
    sh_04_第1个函数改造
    sh_03_第1个函数
    sh_02_快速体验
    sh_01_九九乘法表
    11_测试模块
    sh_12_转义字符
    sh_11_九九乘法表
    sh_10_嵌套打印小星星
    Mariadb/Redis数据库
    部署django项目
  • 原文地址:https://www.cnblogs.com/yuchuan/p/ChineseNumber2ALB.html
Copyright © 2011-2022 走看看