Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.

For example, 123 -> "One Hundred Twenty Three"

12345 -> "Twelve Thousand Three Hundred Forty Five"

1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

提示:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

解法1

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

    public String numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }
        String result = "";
        int i = 0;
        while (num > 0) {
            if (num % 1000 != 0) {
                result = helper(num % 1000) + thousands[i] + " " + result;
            }
            num /= 1000;
            i++;
        }
        return result.trim();
    }

    private String helper(int num) {
        if (num == 0) {
            return "";
        } else if (num < 20) {
            return lessThan20[num] + " ";
        } else if (num < 100) {
            return tens[num / 10] + " " + helper(num % 10);
        } else {
            return lessThan20[num / 100] + " Hundred " + helper(num % 100);
        }
    }

解法2

/**
     * 按英语读法输出阿拉伯数字对应单词
     * @param num 数字,范围:[0,2^31-1]
     * @return
     */
    public String numberToWords(int num) {

        if (num < 0) {
            return "";
        }

        //数字为0直接返回
        if (num == 0) {
            return "Zero";
        }

        //左起段落
        int segment1 = num / 1000000000;           //段落1:十亿位-千亿位
        int segment2 = num % 1000000000 / 1000000; //段落2:百万位-亿位
        int segment3 = num % 1000000 / 1000;       //段落3:千位-十万位
        int segment4 = num % 1000;                 //段落4:个位-百位

        String result = "";

        if (segment1 > 0) {
            result += numToWordsLessThan1000(segment1) + " " + "Billion";
        }
        if (segment2 > 0) {
            result += numToWordsLessThan1000(segment2) + " " + "Million";
        }
        if (segment3 > 0) {
            result += numToWordsLessThan1000(segment3) + " " + "Thousand";
        }
        if (segment4 > 0) {
            result += numToWordsLessThan1000(segment4);
        }

        return result.trim();
    }

/**
  * 按英语读法输出阿拉伯数字对应单词
  * @param num 数字,范围:(0,1000)
  * @return
  */
private String numToWordsLessThan1000(int num) {
        if (num == 0 || num >= 1000) {
            return "";
        }

        String result = "";
        if (num >= 100) {
            result += numToWordsBase(num / 100) + " " + "Hundred";
        }
        num = num % 100;
        if (num > 20) {
            result += numToWordsBase(num / 10 * 10);
            if (num % 10 != 0) { 
                result += numToWordsBase(num % 10);
            }
        } else if (num > 0) {
            result += numToWordsBase(num);
        }

    return result;
}


/**
     * 按英语读法输出阿拉伯数字对应单词
     * @param num 数字,范围:(0,20)∪{30,40,50,60,70,80,90}
     * @return
     */
    private String numToWordsBase(int num) {
        String result = " ";
        switch (num) {
        case 1: result += "One"; break;
        case 2: result += "Two"; break;
        case 3: result += "Three"; break;
        case 4: result += "Four"; break;
        case 5: result += "Five"; break;
        case 6: result += "Six"; break;
        case 7: result += "Seven"; break;
        case 8: result += "Eight"; break;
        case 9: result += "Nine"; break;
        case 10: result += "Ten"; break;
        case 11: result += "Eleven"; break;
        case 12: result += "Twelve"; break;
        case 13: result += "Thirteen"; break;
        case 14: result += "Fourteen"; break;
        case 15: result += "Fifteen"; break;
        case 16: result += "Sixteen"; break;
        case 17: result += "Seventeen"; break;
        case 18: result += "Eighteen"; break;
        case 19: result += "Nineteen"; break;
        case 20: result += "Twenty"; break;
        case 30: result += "Thirty"; break;
        case 40: result += "Forty"; break;
        case 50: result += "Fifty"; break;
        case 60: result += "Sixty"; break;
        case 70: result += "Seventy"; break;
        case 80: result += "Eighty"; break;
        case 90: result += "Ninety"; break;
        }
        return result;
    }

results matching ""

    No results matching ""