-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathInteger to English Words.java
32 lines (26 loc) · 1.36 KB
/
Integer to English Words.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
private static final int[] INT_NUMBERS = {
1_000_000_000, 1_000_000, 1000, 100, 90, 80, 70, 60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
private static final String[] STRING_NUMBERS = {
"Billion", "Million", "Thousand", "Hundred", "Ninety", "Eighty", "Seventy", "Sixty", "Fifty", "Forty", "Thirty", "Twenty",
"Nineteen", "Eighteen", "Seventeen", "Sixteen", "Fifteen", "Fourteen", "Thirteen", "Twelve", "Eleven", "Ten",
"Nine", "Eight", "Seven", "Six", "Five", "Four", "Three", "Two", "One"};
public String numberToWords(int num) {
if (num == 0) return "Zero";
return numberToWordsHelper(num).toString();
}
private StringBuilder numberToWordsHelper(int num) {
StringBuilder sb = new StringBuilder();
if (num == 0) return sb;
for (int i = 0; i < INT_NUMBERS.length; i++) {
if (num >= INT_NUMBERS[i]) {
if (num >= 100) {
sb.append(numberToWordsHelper(num / INT_NUMBERS[i]).append(" "));
}
sb.append(STRING_NUMBERS[i]).append(" ").append(numberToWordsHelper(num % INT_NUMBERS[i]));
break;
}
}
return sb.charAt(sb.length() - 1) == ' ' ? sb.deleteCharAt(sb.length() - 1) : sb; // trim
}
}