Integer to English Words
给一个数字, 返回英文表示. 从大到小判断呗..
public class Solution {
String[] digits = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String int2string(int n){
if (n >= 1000000000) {
return int2string(n / 1000000000) + " Billion" + int2string(n % 1000000000);
} else if (n >= 1000000) {
return int2string(n / 1000000) + " Million" + int2string(n % 1000000);
} else if (n >= 1000) {
return int2string(n / 1000) + " Thousand" + int2string(n % 1000);
} else if (n >= 100) {
return int2string(n / 100) + " Hundred" + int2string(n % 100);
} else if (n >= 20) {
return " " + tens[n / 10] + int2string(n % 10);
} else if (n >= 1) {
return " " + digits[n];
} else {
return "";
}
}
String numberToWords(int num) {
if (num == 0) {
return "Zero";
} else {
String ret = int2string(num);
return ret.trim();
}
}
}