中国語の数字がアラビア数字に変わる(最大1兆元を超えない)
4062 ワード
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DigitUtil {
private static Map<String, Integer> MAP_ALPHA = new HashMap<String, Integer>();
static{
MAP_ALPHA.put(" ", 1);
MAP_ALPHA.put(" ", 2);
MAP_ALPHA.put(" ", 3);
MAP_ALPHA.put(" ", 4);
MAP_ALPHA.put(" ", 5);
MAP_ALPHA.put(" ", 6);
MAP_ALPHA.put(" ", 7);
MAP_ALPHA.put(" ", 8);
MAP_ALPHA.put(" ", 9);
}
/**
* 1
* @param text
*/
public static long getArabicDigit(String text){
int b=text.length();
if (b>23) {
return 0;
}
long count = 0;
if (text.indexOf(" ")>-1) {//text>1
String[]u = text.split(" ");
String num_yi = u[0];
int yi = getThousands(num_yi);
count+=yi*100000000l;
if (u.length>1) {
if (u[1].indexOf(" ")>-1) {
String[]v = u[1].split(" ");
String num_wan = v[0];
int wan = getThousands(num_wan);
count+=wan*10000;
if (v.length>1) {
String num = v[1];
int p = getThousands(num);
count+=p;
}
}else {
int p = getThousands(u[1]);
count+=p;
}
}
}else if (text.indexOf(" ")>-1) {//text<1 &&text>1
String[]v = text.split(" ");
String num_wan = v[0];
int wan = getThousands(num_wan);
count+=wan*10000;
if (v.length>1) {
String num = v[1];
int p = getThousands(num);
count+=p;
}
}else {//text<1 &&text>0
int p = getThousands(text);
count+=p;
}
return count;
}
/**
*
*/
public static int getThousands(String text){
text = text.replaceAll(" ", "");
int count = 0;
if (text.contains(" ")) {
String u = getUnitDigit(text, " ");
text = text.replaceAll(u, "");
if (u.length()>0) {
int qian = MAP_ALPHA.get(u.replaceAll(" ", ""));
count+=qian*1000;
}
}
if (text.contains(" ")) {
String u = getUnitDigit(text, " ");
text = text.replaceAll(u, "");
if (u.length()>0) {
int bai = MAP_ALPHA.get(u.replaceAll(" ", ""));
count+=bai*100;
}
}
if (text.contains(" ")) {
String u = getUnitDigit(text, " ");
text = text.replaceAll(u, "");
if (u.length()==0) {
u=" ";
}
if (u.length()>0) {
int shi = MAP_ALPHA.get(u.replaceAll(" ", ""));
count+=shi*10;
}
}
if (MAP_ALPHA.keySet().contains(text)) {
int ge = MAP_ALPHA.get(text);
count+=ge;
}
return count;
}
/**
*
* @param text
* @param unit
* @return
*/
public static String getUnitDigit(String text,String unit) {
String regexp = "[ ]{1}"+unit;
Pattern pattern = Pattern.compile(regexp, 2);
Matcher matcher = pattern.matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
sb.append(matcher.group());
}
return sb.toString();
}
// 2: ,split” “,2
public static void main(String[] args) {
String t = " ";
try {
long s = getArabicDigit(t);
System.out.println(s);
} catch (Exception e) {
// e.printStackTrace();
System.out.println("chinese digit not support");
}
}
}