给定一个整数,打印该整数的英文描述。

示例 1:

输入: 123
输出: “One Hundred Twenty Three”
示例 2:

输入: 12345
输出: “Twelve Thousand Three Hundred Forty Five”
示例 3:

输入: 1234567
输出: “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
示例 4:

输入: 1234567891
输出: “One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”

java代码:

class Solution {
    private static String[] UNITS={"Thousand","Million","Billion"};
    private static String[] ONE={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine", "Ten", "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    private static String[] TWO={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};

    public String numberToWords(int num) {
        if(num==0) return "Zero";
        String str="";int i=0;
        while(num!=0){
            String ret = getString(num%1000);
            if(i!=0 && !ret.isEmpty()) ret+=" "+UNITS[i-1];
            if(!ret.isEmpty() && !str.isEmpty()) ret+=" ";
            str=ret+str;i++;num/=1000;
        }
        return str;
    }

    private String getString(int num){
        String str="";int value = num%100;
        if(value==0);
        else if(value<20)str+=ONE[value-1];
        else str+= TWO[value/10-2]+((value%10)==0? "":(" "+ONE[(value%10)-1]));
        if(num>=100) str=ONE[num/100-1]+" "+"Hundred"+(str.isEmpty()?str:(" "+str));
        return str;
    }
}