import java.util.regex.Matcher; import java.util.regex.Pattern; /** *<p>中文数字转阿拉伯数字/p> */ public class CN2Num { static char[] cnArr = new char [] {'一','二','三','四','五','六','七','八','九', '两'}; static char[] chArr = new char [] {'十','百','千','万','亿'}; static String allChineseNum = "零一二三四五六七八九十百千万亿0123456789两"; public static int cn2Num(String chineseNum) { int result = 0; int temp = 1; int count = 0; for (int i = 0; i < chineseNum.length(); i++) { boolean b = true; char c = chineseNum.charAt(i); for (int j = 0; j < cnArr.length; j++) { if (c == cnArr[j]) { if(0 != count){ result += temp; temp = 1; count = 0; } // 下标+1,就是对应的值 if (c == '两') { temp = 2; } else { temp = j + 1; } b = false; break; } } if(b){ for (int j = 0; j < chArr.length; j++) { if (c == chArr[j]) { switch (j) { case 0: temp *= 10; break; case 1: temp *= 100; break; case 2: temp *= 1000; break; case 3: temp *= 10000; break; case 4: temp *= 100000000; break; default: break; } count++; } } } if (i == chineseNum.length() - 1) { result += temp; } } return result; } public static boolean isChineseNum(String chineseStr) { char [] ch = chineseStr.toCharArray(); for (char c : ch) { if (!allChineseNum.contains(String.valueOf(c))) { return false; } } return true; } public static boolean isNum(String str) { String reg = "[0-9\\.]+"; return str.matches(reg); } public static String extractNum(String str) { String reg = "[0-9\\.]+"; Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(str); if (matcher.find()) { return matcher.group(); } return null; } public static String extractUnit(String str) { String reg = "十|百|千|万|亿"; Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(str); if (matcher.find()) { return matcher.group(); } return null; } }
JAVA 中文数字转阿拉伯数字
最新推荐文章于 2023-08-01 10:36:20 发布