MyException - 我的异常网
当前位置:我的异常网» J2SE » 求Java 数目字转成英文 需要原创 + 注释 急300分

求Java 数目字转成英文 需要原创 + 注释 急300分(7)

www.MyException.Cn  网友分享于:2013-12-19  浏览:67次

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

public class Decode {

    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");
    }
}

------解决方案--------------------
这么多代码。。。我不贴了,头都晕了
------解决方案--------------------
为了两分..........
------解决方案--------------------
顶一下吧
------解决方案--------------------
都写出来,顶一下吧!
------解决方案--------------------
很容易啊,跟做日历一样的
比日历还简单呢
------解决方案--------------------
看看谁得算法简练
------解决方案--------------------
有难度,
------解决方案--------------------

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有