package com.baijob.vsearch.util;
可以转换一下格式的数字
三千五
二万三千五百三
3千4百
import java.util.*;
public class DigitUtil {
private static Set<</SPAN>Character> araDigits = new HashSet<</SPAN>Character>();
private static char[] SCDigits = {'零','一','二','三','四','五','六','七','八','九','十','百','千','万','亿'};
private static char[] TCDigits = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾','佰','仟','万','亿'};
private static Map<</SPAN>Character,Character> map = new HashMap<</SPAN>Character,Character>();
static {
for (int i = 0; i <</SPAN> TCDigits.length; i++) {
map.put(TCDigits[i], SCDigits[i]);
}
for (char i = '0'; i <= '9'; i++) {
araDigits.add(i);
}
}
private DigitUtil(){
}
public static void main(String[] args) {
System.out.println(parseDigits("零三"));
}
public static Integer parseDigits(String hanzi) {
if (!isDigits(hanzi))
return null;
int ret;
try {
if (hanzi.charAt(0) == '+')
hanzi = hanzi.substring(1);
ret = Integer.parseInt(hanzi);
} catch (Exception e) {
char[] chars = hanzi.toCharArray();
changeTCtoSC(chars);
ret = parse(chars,0,chars.length,1);
}
return ret;
}
public static boolean isDigits(String s) {
if (s.charAt(0) == '+')
s = s.substring(1);
try {
Integer.parseInt(s);
return true;
} catch (Exception e) {
for (int i = 0; i <</SPAN> s.length(); i++) {
char c = s.charAt(i);
if (!map.values().contains(c) && !araDigits.contains(c))
return false;
}
return true;
}
}
private static int parse(char[] chars,int start,int end, int preNumber) {
int ret = 0;
if (start == end) {
ret = 0;
} else if (start + 1 == end) {
switch (chars[start]) {
case '一':
case '1':
ret = 1 * preNumber;
break;
case '二':
case '2':
ret = 2 * preNumber;
break;
case '三':
case '3':
ret = 3 * preNumber;
break;
case '四':
case '4':
ret = 4 * preNumber;
break;
case '五':
case '5':
ret = 5 * preNumber;
break;
case '六':
case '6':
ret = 6 * preNumber;
break;
case '七':
case '7':
ret = 7 * preNumber;
break;
case '八':
case '8':
ret = 8 * preNumber;
break;
case '九':
case '9':
ret = 9 * preNumber;
break;
}
} else {
int index;
if ((index = indexOf(chars,start,end,'零')) == 0 || (index = indexOf(chars,start,end,'0')) == 0) {
ret = parse(chars, start + 1, end, 1);
} else if ((index = indexOf(chars,start,end,'亿')) != -1) {
ret = parse(chars, start,index, 1) * 100000000 + parse(chars,index + 1,end,10000000);
} else if ((index = indexOf(chars,start,end,'万')) != -1) {
ret = parse(chars, start,index, 1) * 10000 + parse(chars,index + 1,end,1000);
} else if ((index = indexOf(chars,start,end,'千')) != -1) {
ret = parse(chars, start, index, 1) * 1000 + parse(chars,index + 1,end,100);
} else if ((index = indexOf(chars,start,end,'百')) != -1) {
ret = parse(chars, start, index, 1) * 100 + parse(chars,index + 1,end,10);
} else if ((index = indexOf(chars,start,end,'十')) != -1) {
ret = parse(chars, start, index, 1) * 10 + parse(chars,index + 1,end,1);
}
}
return ret;
}
private static int indexOf(char[] chars, int start, int end, char c) {
for (int i = start; i <</SPAN> end; i++) {
if (chars[i] == c)
return i;
}
return -1;
}
private static void changeTCtoSC(char[] chars) {
for (int i = 0; i <</SPAN> chars.length; i++) {
Character c = map.get(chars[i]);
if (c != null)
chars[i] = c;
}
}
} |
|