英文整数数字转成阿拉伯数字,正负 百万亿以内
测试结果:
"negative four hundred nineteen trillion two hundred thirty four billion five hundred sixty seven million eight hundred ninety zero thousand one hundred"
转换后为
-419234567890100
源码:
import java.util.HashMap;
public class EnNum2Num {
private static HashMap<String,Long> map = new HashMap();
static {
map.put("negative", -1L);
map.put("zero", 0L);
map.put("one", 1L);
map.put("two", 2L);
map.put("three", 3L);
map.put("four", 4L);
map.put("five", 5L);
map.put("six", 6L);
map.put("seven", 7L);
map.put("eight", 8L);
map.put("nine", 9L);
map.put("ten", 10L);
map.put("eleven", 11L);
map.put("twelve", 12L);
map.put("thirteen", 13L);
map.put("fourteen", 14L);
map.put("fifteen", 15L);
map.put("sixteen", 16L);
map.put("seventeen", 17L);
map.put("eighteen", 18L);
map.put("nineteen", 19L);
map.put("twenty", 20L);
map.put("thirty", 30L);
map.put("forty", 40L);
map.put("fifty", 50L);
map.put("sixty", 60L);
map.put("seventy", 70L);
map.put("eighty", 80L);
map.put("ninety", 90L);
map.put("hundred", 100L);
map.put("thousand", 1000L);
map.put("million", 1000000L);
map.put("billion", 1000000000L);
map.put("trillion",1000000000000L);
}
private static long underHundred(String s){
if(s == null){
return 0;
}
s = s.trim();
if(s.equals("")){
return 0;
}
long sum = 0;
String[] arr = s.split(" ");
for (String a : arr) {
if(a.equals("")){
continue;
}
sum += map.get(a);
}
return sum;
}
private static long underThousand(String s) throws Exception {
if(s == null){
return 0;
}
s = s.trim();
if(s.equals("")){
return 0;
}
long sum = 0;
if(s.contains("hundred")) {
String[] arr = s.split("hundred");
if (arr.length == 2) {
sum += underHundred(arr[0]) * map.get("hundred");
s = arr[1];
} else if (arr.length == 3) {
throw new Exception("2 hundred");
} else if (arr.length == 1) {
sum += underHundred(arr[0]) * map.get("hundred");
return sum;
}
}
sum += underHundred(s);
// System.out.println("sum="+sum);
return sum;
}
private static long underMillion(String s) throws Exception {
if(s == null){
return 0;
}
s = s.trim();
if(s.equals("")){
return 0;
}
long sum = 0;
if(s.contains("thousand")) {
String[] arr = s.split("thousand");
if (arr.length == 2) {
sum += underThousand(arr[0]) * map.get("thousand");
s = arr[1];
} else if (arr.length == 3) {
throw new Exception("2 thousand");
} else if (arr.length == 1) {
sum += underThousand(arr[0]) * map.get("thousand");
return sum;
}
}
sum += underThousand(s);
// System.out.println("sum="+sum);
return sum;
}
private static long underBillion(String s) throws Exception {
if(s == null){
return 0;
}
s = s.trim();
if(s.equals("")){
return 0;
}
long sum = 0;
if(s.contains("million")){
String[] arr = s.split("million");
if(arr.length == 2){
sum += underThousand(arr[0])*map.get("million");
s = arr[1];
}else if(arr.length == 3){
throw new Exception("2 million");
}else if(arr.length == 1){
sum += underThousand(arr[0])*map.get("million");
return sum;
}
}
sum += underMillion(s);
// System.out.println("sum="+sum);
return sum;
}
private static long underTrillion(String s) throws Exception {
if(s == null){
return 0;
}
s = s.trim();
if(s.equals("")){
return 0;
}
long sum = 0;
if(s.contains("billion")) {
String[] arr = s.split("billion");
if (arr.length == 2) {
sum += underThousand(arr[0]) * map.get("billion");
s = arr[1];
} else if (arr.length == 3) {
throw new Exception("2 billion");
} else if (arr.length == 1) {
sum += underThousand(arr[0]) * map.get("billion");
return sum;
}
}
sum += underBillion(s);
// System.out.println("sum="+sum);
return sum;
}
//enNum 必须是一个英文数字 ,不能掺杂无关单词
//注意:没有处理在中间的符号 '-' ,有些数字有写法如 fifty-four
public static long enNum2Num(String s) throws Exception {
if(s == null || s.trim().equals("")){
return 0;
}
s = s.replaceAll(" and "," ");
s = s.replaceAll(" ","");
long flag = 1;
if(s.contains("negative")){
flag = -1;
s = s.replace("negative","").trim();
}
long sum = 0;
if(s.contains("trillion")) {
String[] arr = s.split("trillion");
if (arr.length == 2) {
sum += underThousand(arr[0]) * map.get("trillion");
s = arr[1];
} else if (arr.length == 3) {
throw new Exception("2 trillion");
} else if (arr.length == 1) {
sum += underThousand(arr[0]) * map.get("trillion");
return sum;
}
}
sum += underTrillion(s);
return sum*flag;
}
public static void main(String[] args) throws Exception {
String s = "negative four hundred nineteen trillion two hundred thirty four billion five hundred sixty seven million eight hundred ninety zero thousand one hundred";
long l = enNum2Num(s);
System.out.println(l);
}
}