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

      为了便于以后重用,现把代码贴出来


     1 import java.math.BigInteger;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 import java.util.regex.Matcher;
     5 import java.util.regex.Pattern;
     6 
     7 /**
     8  * 模块: 描述:将汉字数字转换为数字
     9  * 
    10  * @author 李乐 601235723@qq.com
    11  */
    12 public class ChineseNumber {
    13     private ChineseNumber() {
    14     }
    15 
    16     private static Pattern CHN_NUM_PATTERN = Pattern.compile("[一二三四五六七八九][十百千]?");
    17     private static Map<Character, Integer> CHN_UNITS = new HashMap<Character, Integer>();
    18     private static Map<Character, Integer> CHN_NUMS = new HashMap<Character, Integer>();
    19     static {
    20         CHN_UNITS.put('十', 10);
    21         CHN_UNITS.put('百', 100);
    22         CHN_UNITS.put('千', 1000);
    23         CHN_UNITS.put('万', 10000);
    24         CHN_UNITS.put('亿', 10000000);
    25         CHN_NUMS.put('一', 1);
    26         CHN_NUMS.put('二', 2);
    27         CHN_NUMS.put('三', 3);
    28         CHN_NUMS.put('四', 4);
    29         CHN_NUMS.put('五', 5);
    30         CHN_NUMS.put('六', 6);
    31         CHN_NUMS.put('七', 7);
    32         CHN_NUMS.put('八', 8);
    33         CHN_NUMS.put('九', 9);
    34     }
    35 
    36     /**
    37      * 将小于一万的汉字数字,转换为BigInteger
    38      * 
    39      * @param chnNum
    40      * @return
    41      */
    42     private static BigInteger getNumber(String chnNum) {
    43         BigInteger number = BigInteger.valueOf(0);
    44         Matcher m = CHN_NUM_PATTERN.matcher(chnNum);
    45         m.reset(chnNum);
    46         while (m.find()) {
    47             String subNumber = m.group();
    48             if (subNumber.length() == 1) {
    49                 number = number.add(BigInteger.valueOf(CHN_NUMS.get(subNumber.charAt(0))));
    50             } else if (subNumber.length() == 2) {
    51                 number = number.add(BigInteger.valueOf(CHN_NUMS.get(subNumber.charAt(0))).multiply(BigInteger.valueOf(CHN_UNITS.get(subNumber.charAt(1)))));
    52             }
    53         }
    54         return number;
    55     }
    56 
    57     /**
    58      * 将汉字转换为数字
    59      * 
    60      * @param num
    61      * @return
    62      */
    63     public static int parseNumber(String chnNum) {
    64         chnNum = chnNum.replaceAll("(?<![一二三四五六七八九])十", "一十").replaceAll("零", "");
    65         Pattern pattern = Pattern.compile("[万亿]");
    66         Matcher m = pattern.matcher(chnNum);
    67         BigInteger result = BigInteger.valueOf(0);
    68         int index = 0;
    69         while (m.find()) {
    70             int end = m.end();
    71             int multiple = CHN_UNITS.get(m.group().charAt(0));
    72             String num = chnNum.substring(index, m.start());
    73             result = result.add(getNumber(num)).multiply(BigInteger.valueOf(multiple));
    74             index = end;
    75         }
    76         String num = chnNum.substring(index);
    77         result = result.add(getNumber(num));
    78         return result.intValue();
    79     }
    80 }

     还有不完善的地方,凑合能用。

  • 相关阅读:
    python并发编程的几种方法
    pycharm pytest 运行时报错 gbk
    mac m1 安装python3
    python json.dumps 打印出后为乱码 解决方法
    git ssh密匙配置
    登录接口需html中的token时,需用requests-html库
    代码服务器运行时找不到包文件位置
    mac终端使用iterm及主题 高亮
    Mac 生成项目目录树形结构
    mac 安装xcode命令行工具
  • 原文地址:https://www.cnblogs.com/lmtoo/p/2828604.html
Copyright © 2011-2022 走看看