-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIntegerToEnglishWords.java
More file actions
42 lines (41 loc) · 1.87 KB
/
IntegerToEnglishWords.java
File metadata and controls
42 lines (41 loc) · 1.87 KB
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
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
public String numberToWords(int num) {
if (num == 0) return "Zero";
int billions = num / 1000000000;
num = num % 1000000000;
int millions = num / 1000000;
num = num % 1000000;
int thousands = num / 1000;
num = num % 1000;
StringBuilder sb = new StringBuilder();
if (billions > 0) sb.append(numberToWordsUnderThousand(billions) + " Billion ");
if (millions > 0) sb.append(numberToWordsUnderThousand(millions) + " Million ");
if (thousands > 0) sb.append(numberToWordsUnderThousand(thousands) + " Thousand ");
if (num > 0) sb.append(numberToWordsUnderThousand(num));
return sb.toString().trim();
}
public String numberToWordsUnderThousand(int num) {
int hundreds = num / 100;
num = num % 100;
int tens = num / 10;
num = num % 10;
StringBuilder sb = new StringBuilder();
String[] tenWords = new String[] {"Twenty", "Thirty", "Forty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninety"
};
String[] words = new String[] {"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen"
};
if (hundreds > 0) sb.append(words[hundreds - 1] + " Hundred ");
if (tens > 1) {
sb.append(tenWords[tens - 2] + " ");
if (num > 0) sb.append(words[num - 1]);
} else if (tens * 10 + num > 0) {
sb.append(words[tens * 10 + num - 1]);
}
return sb.toString().trim();
}
}