转换中文数字为阿拉伯数字
可以转换以下格式的数字:
一百
三千五
二万三千五百三
3千4百
1 package com.baijob.vsearch.util; 2 3 import java.util.*; 4 5 /** 6 * 处理数词的工具类 7 * @author liushiquan 8 * 9 */ 10 public class DigitUtil { 11 /** 12 * 阿拉伯数字 13 */ 14 private static Set<Character> araDigits = new HashSet<Character>(); 15 /** 16 * 汉字中的数字字符 17 */ 18 private static char[] SCDigits = {'零','一','二','三','四','五','六','七','八','九','十','百','千','万','亿'}; 19 20 /** 21 * 汉字中的大写数字字符 22 */ 23 private static char[] TCDigits = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾','佰','仟','万','亿'}; 24 /** 25 * 繁体中文和简体中文的对应关系 26 */ 27 private static Map<Character,Character> map = new HashMap<Character,Character>(); 28 static { 29 for (int i = 0; i < TCDigits.length; i++) { 30 map.put(TCDigits[i], SCDigits[i]); 31 } 32 for (char i = '0'; i <= '9'; i++) { 33 araDigits.add(i); 34 } 35 } 36 private DigitUtil(){ 37 38 } 39 public static void main(String[] args) { 40 System.out.println(parseDigits("零三")); 41 } 42 /** 43 * 解析中文格式的数字,假定参数中全是汉字,否则会解析异常,解析失败返回null 44 * @param hanzi 45 * @return 46 */ 47 public static Integer parseDigits(String hanzi) { 48 if (!isDigits(hanzi)) 49 return null; 50 int ret; 51 try { 52 if (hanzi.charAt(0) == '+') 53 hanzi = hanzi.substring(1); 54 55 ret = Integer.parseInt(hanzi); 56 } catch (Exception e) { 57 58 char[] chars = hanzi.toCharArray(); 59 changeTCtoSC(chars); 60 61 ret = parse(chars,0,chars.length,1); 62 } 63 64 return ret; 65 } 66 public static boolean isDigits(String s) { 67 if (s.charAt(0) == '+') 68 s = s.substring(1); 69 try { 70 Integer.parseInt(s); 71 return true; 72 } catch (Exception e) { 73 for (int i = 0; i < s.length(); i++) { 74 char c = s.charAt(i); 75 if (!map.values().contains(c) && !araDigits.contains(c)) 76 return false; 77 } 78 79 return true; 80 } 81 } 82 private static int parse(char[] chars,int start,int end, int preNumber) { 83 int ret = 0; 84 if (start == end) { 85 ret = 0; 86 } else if (start + 1 == end) { 87 switch (chars[start]) { 88 case '一': 89 case '1': 90 ret = 1 * preNumber; 91 break; 92 case '二': 93 case '2': 94 ret = 2 * preNumber; 95 break; 96 case '三': 97 case '3': 98 ret = 3 * preNumber; 99 break; 100 case '四': 101 case '4': 102 ret = 4 * preNumber; 103 break; 104 case '五': 105 case '5': 106 ret = 5 * preNumber; 107 break; 108 case '六': 109 case '6': 110 ret = 6 * preNumber; 111 break; 112 case '七': 113 case '7': 114 ret = 7 * preNumber; 115 break; 116 case '八': 117 case '8': 118 ret = 8 * preNumber; 119 break; 120 case '九': 121 case '9': 122 ret = 9 * preNumber; 123 break; 124 } 125 } else { 126 int index; 127 if ((index = indexOf(chars,start,end,'零')) == 0 || (index = indexOf(chars,start,end,'0')) == 0) { 128 ret = parse(chars, start + 1, end, 1); 129 } else if ((index = indexOf(chars,start,end,'亿')) != -1) { 130 ret = parse(chars, start,index, 1) * 100000000 + parse(chars,index + 1,end,10000000); 131 } else if ((index = indexOf(chars,start,end,'万')) != -1) { 132 ret = parse(chars, start,index, 1) * 10000 + parse(chars,index + 1,end,1000); 133 } else if ((index = indexOf(chars,start,end,'千')) != -1) { 134 ret = parse(chars, start, index, 1) * 1000 + parse(chars,index + 1,end,100); 135 } else if ((index = indexOf(chars,start,end,'百')) != -1) { 136 ret = parse(chars, start, index, 1) * 100 + parse(chars,index + 1,end,10); 137 } else if ((index = indexOf(chars,start,end,'十')) != -1) { 138 ret = parse(chars, start, index, 1) * 10 + parse(chars,index + 1,end,1); 139 } 140 141 } 142 return ret; 143 } 144 private static int indexOf(char[] chars, int start, int end, char c) { 145 for (int i = start; i < end; i++) { 146 if (chars[i] == c) 147 return i; 148 } 149 return -1; 150 } 151 /** 152 * 将繁体中文转换为简体中文 153 * @param chars 154 */ 155 private static void changeTCtoSC(char[] chars) { 156 for (int i = 0; i < chars.length; i++) { 157 Character c = map.get(chars[i]); 158 if (c != null) 159 chars[i] = c; 160 } 161 } 162 } 163 //该片段来自于http://outofmemory.cn
如今的编程是一场程序员和上帝的竞赛,程序员要开发出更大更好、傻瓜都会用到软件。而上帝在努力创造出更大更傻的傻瓜。目前为止,上帝是赢的。
QQ:6203142
-----在北京的四川小伙