|
13 | 13 | 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.
|
14 | 14 | 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)
|
15 | 15 | */
|
16 |
| -public class IntegertoEnglishWords { |
| 16 | +public class _273 { |
17 | 17 |
|
18 |
| - private String[] digit = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; |
19 |
| - private String[] teen = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; |
20 |
| - private String[] ten = new String[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; |
21 |
| - private String[] thousand = new String[] {"Thousand", "Million", "Billion"}; |
| 18 | + private String[] belowTen = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; |
| 19 | + private String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; |
| 20 | + private String[] belowHundred = new String[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; |
| 21 | + private String[] overThousand = new String[] {"Thousand", "Million", "Billion"}; |
22 | 22 |
|
23 | 23 | public String numberToWords(int num) {
|
24 |
| - String ans; |
25 |
| - if (num == 0) |
26 |
| - return digit[num]; |
| 24 | + String result; |
| 25 | + if (num == 0) return belowTen[num]; |
27 | 26 |
|
28 |
| - ans = hundredHelper(num%1000); |
| 27 | + result = hundredHelper(num%1000); |
29 | 28 | num = num/1000;
|
30 | 29 | int i = 0;
|
31 | 30 | while (i < 3 && num > 0) {
|
32 |
| - if (num % 1000 > 0) |
33 |
| - ans = hundredHelper(num%1000) + thousand[i] + " " + ans; |
| 31 | + if (num % 1000 > 0) { |
| 32 | + result = hundredHelper(num % 1000) + overThousand[i] + " " + result; |
| 33 | + } |
34 | 34 | num = num/1000;
|
35 | 35 | i++;
|
36 | 36 | }
|
37 | 37 |
|
38 |
| - return ans.trim(); |
| 38 | + return result.trim(); |
39 | 39 | }
|
40 | 40 |
|
41 | 41 | public String hundredHelper(int num) {
|
42 | 42 | String nstr = "";
|
43 | 43 | if (num >= 100) {
|
44 |
| - nstr = digit[num/100] + " Hundred "; |
| 44 | + nstr = belowTen[num/100] + " Hundred "; |
45 | 45 | }
|
46 | 46 | num = num%100;
|
47 | 47 | if (num >= 20) {
|
48 |
| - if (num % 10 != 0) |
49 |
| - nstr = nstr + ten[num/10 - 2] + " " + digit[num%10] + " "; |
50 |
| - else |
51 |
| - nstr = nstr + ten[num/10 - 2] + " "; |
| 48 | + if (num % 10 != 0) { |
| 49 | + nstr = nstr + belowHundred[num / 10 - 2] + " " + belowTen[num % 10] + " "; |
| 50 | + } |
| 51 | + else { |
| 52 | + nstr = nstr + belowHundred[num / 10 - 2] + " "; |
| 53 | + } |
52 | 54 | } else if (num >= 10) {
|
53 |
| - nstr = nstr + teen[num%10] + " "; |
| 55 | + nstr = nstr + belowTwenty[num%10] + " "; |
54 | 56 | } else if (num > 0){
|
55 |
| - nstr = nstr + digit[num] + " "; |
| 57 | + nstr = nstr + belowTen[num] + " "; |
56 | 58 | }
|
57 | 59 | return nstr;
|
58 | 60 | }
|
|
0 commit comments