当前位置: 代码迷 >> J2SE >> 数字的英文表达转成阿拉伯数字~求教,该怎么处理
  详细解决方案

数字的英文表达转成阿拉伯数字~求教,该怎么处理

热度:17   发布时间:2016-04-24 01:06:43.0
数字的英文表达转成阿拉伯数字~~~~~求教
我现在要实现把one thousand seven hundred and thirty one 这种英文表达方式改为1731,有代码实现吗?求教。

------解决方案--------------------
Java code
import java.util.HashMap;public class Test {    static HashMap<String, Integer> numMap = new HashMap<String, Integer>();    static HashMap<String, Long> unitMap = new HashMap<String, Long>();        private static void init(){        numMap.put("one", 1);        numMap.put("two", 2);        numMap.put("three", 3);        numMap.put("four", 4);        numMap.put("five", 5);        numMap.put("six", 6);        numMap.put("seven", 7);        numMap.put("eight", 8);        numMap.put("nine", 9);        numMap.put("ten", 10);        numMap.put("eleven", 11);        numMap.put("twelve", 12);        numMap.put("thirteen", 13);        numMap.put("fourteen", 14);        numMap.put("fifteen", 15);        numMap.put("sixteen", 16);        numMap.put("seventeen", 17);        numMap.put("eighteen", 18);        numMap.put("nineteen", 19);        numMap.put("twenty", 20);        numMap.put("thirty", 30);        numMap.put("forty", 40);        numMap.put("fifty", 50);        numMap.put("sixty", 60);        numMap.put("seventy", 70);        numMap.put("eighty", 80);        numMap.put("ninety", 90);                unitMap.put("hundred", 100L);        unitMap.put("thousand", 1000L);        unitMap.put("million", 1000000L);        unitMap.put("billion", 1000000000L);    }        public static void main(String[] args) {        init();        //121,942,005,731        String string = "one hundred and twenty-one billion nine hundred and forty-two million five " +                "thousand seven hundred and thirty one";        long num = getNum(string);        System.out.println(Long.valueOf(String.valueOf(num)));        //格式化        System.out.println(formatNum(num));    }    static long getNum(String string){        final String ignoreString = "and";        string = string.trim();        string = string.replaceAll("-", " ");        String[] strings = string.split(" ");        long unit = 0;        long sub = 0,sum = 0;        for(int i = 0;i < strings.length;i++){            if(unitMap.containsKey(strings[i])){                if(strings[i].equals("hundred") && checkHundred(strings, i)){                    sub *= unitMap.get(strings[i]);                }else{                    unit = unitMap.get(strings[i]);                    sum += sub * unit;                    sub = 0;                }            }            else if (numMap.containsKey(strings[i])) {                sub += numMap.get(strings[i]);            }            else if (strings[i].equals(ignoreString)) {                continue;            }            else {                System.out.println("error!");                return Integer.MIN_VALUE;            }        }        sum += sub;        return sum;    }        static boolean checkHundred(String[] strings,int index){        for(int i = index + 1;i < strings.length;i++){            if(strings[i].endsWith("hundred")){                return true;            }        }        return false;    }        static String formatNum(long num){        StringBuffer sb = new StringBuffer(String.valueOf(num));        StringBuffer result = new StringBuffer();        int count =  0;        for(int i = sb.length() - 1;i >= 0;i--){            result.append(sb.charAt(i));            ++count;            if(count % 3 == 0 && i != 0){                result.append(",");            }        }        return result.reverse().toString();            }}
  相关解决方案