日期:2014-05-20  浏览次数:20825 次

求Java 数字转成英文 需要原创 + 注释 急!!!300分
要求如下,先到先得。 

需要原创! 1楼的供参考


"decode 1" outputs "one"

"decode 21" outputs "twenty one"

"decode 105" outputs "one hundred and five"

"decode 56945781" outputs "fifty six million nine hundred and forty five thousand seven hundred and eighty one"

The application should be able to handle any number from the range 0 - 999999999.

------解决方案--------------------
我英语四级还没过,下面的一堆单词我只认识code java static string thousand million billion七个单词
Java code

 static String[] denom = { "", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion",
            "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "sexdecillion", "septendecillion", "octodecillion",
            "novemdecillion", "vigintillion" };

------解决方案--------------------
Java code

public class Main {

    static String[] to_19 = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
            "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    static String[] tens = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    static String[] denom = { "", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion",
            "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "sexdecillion", "septendecillion", "octodecillion",
            "novemdecillion", "vigintillion" };

    public static void main(String[] argv) throws Exception {
        int tstValue = 111199697;
        Main_app itoe = new Main_app();
        System.out.println(itoe.english_number(tstValue));
    }

    private String convert_nn(int val) throws Exception {
        if (val < 20)
            return to_19[val];

        int flag = val / 10 - 2;
        if (val % 10 != 0)
            return tens[flag] + "-" + to_19[val % 10];
        else
            return tens[flag];
    }

    private String convert_nnn(int val) throws Exception {
        String word = "";
        int rem = val / 100;
        int mod = val % 100;
        if (rem > 0) {
            word = to_19[rem] + " hundred";
            if (mod > 0) {
                word = word + " ";
            }
        }
        if (mod > 0) {
            word = word + convert_nn(mod);
        }
        return word;
    }

    public String english_number(int val) throws Exception {
        if (val < 100) {
            return convert_nn(val);
        }
        if (val < 1000) {
            return convert_nnn(val);
        }
        for (int v = 0; v < denom.length; v++) {
            int didx = v - 1;
            int dval = new Double(Math.pow(1000, v)).intValue();
            if (dval > val) {
                int mod = new Double(Math.pow(1000, didx)).intValue();
                int l = val / mod;
                int r = val - (l * mod);
                String ret = convert_nnn(l) + " " + denom[didx];
                if (r > 0) {
                    ret = ret + ", " + english_number(r);
                }
                return ret;
            }
        }
        throw new Exception("Should never get here, bottomed out in english_number");
    }
}