-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerEnglishWords.java
141 lines (125 loc) · 3.79 KB
/
IntegerEnglishWords.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package techQuestions;
public class IntegerEnglishWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 0;
System.out.println(numberToWords(num));
}
public static String numberToWords(int num) {
String output = "";
if (num == 0) {
return "Zero";
}
int billion = num / 1000000000;
if (billion >= 1) {
System.out.println(billion);
output = ones(billion) + " " + "Billion";
}
int million = num - (billion * 1000000000);
million = million / 1000000;
if (million >= 1) {
if ((million / 100) >= 1) {
output = output + " " + ones(million/100) + " Hundred";
}
if ((million % 100) >= 10 && (million % 100) < 20) {
output = output + " " + tens(million % 100);
} else if ((million % 100) >= 20 && (million % 100) < 100) {
output = output + " " + twentyUp(((million%100)/10)*10);
if (((million%100)%10) >= 0 && ((million%100)%10) <=9) {
output = output + " " + ones((million%100)%10);
}
} else if (((million%100)%10) >= 0 && ((million%100)%10) <=9) {
output = output + " " + ones((million%100)%10);
}
output = output + " Million";
}
int thousand = num - (billion * 1000000000) - (million * 1000000);
thousand = thousand / 1000;
if (thousand >= 1) {
if ((thousand / 100) >= 1) {
output = output + " " + ones(thousand/100) + " Hundred";
}
if ((thousand % 100) >= 10 && (thousand % 100) < 20) {
output = output + " " + tens(thousand % 100);
} else if ((thousand % 100) >= 20 && (thousand % 100) < 100) {
output = output + " " + twentyUp(((thousand%100)/10)*10);
if (((thousand%100)%10) >= 0 && ((thousand%100)%10) <=9) {
output = output + " " + ones((thousand%100)%10);
}
} else if (((thousand%100)%10) >= 0 && ((thousand%100)%10) <=9) {
output = output + " " + ones((thousand%100)%10);
}
output = output + " Thousand";
}
int hundred = num - (billion * 1000000000) - (million * 1000000) - (thousand * 1000);
if (hundred >= 1) {
if ((hundred / 100) >= 1) {
output = output + " " + ones(hundred/100) + " Hundred";
}
if ((hundred % 100) >= 10 && (hundred % 100) < 20) {
output = output + " " + tens(thousand % 100);
} else if ((hundred % 100) >= 20 && (hundred % 100) < 100) {
output = output + " " + twentyUp(((hundred%100)/10)*10);
if (((hundred%100)%10) >= 0 && ((hundred%100)%10) <=9) {
output = output + " " + ones((hundred%100)%10);
}
} else if (((hundred%100)%10) >= 0 && ((hundred%100)%10) <=9) {
output = output + " " + ones((hundred%100)%10);
}
}
return output.stripLeading();
}
public static String twentyUp(int num) {
switch(num) {
case 20: return "Twenty";
case 30: return "Thirty";
case 40: return "Forty";
case 50: return "Fifty";
case 60: return "Sixty";
case 70: return "Seventy";
case 80: return "Eighty";
case 90: return "Ninety";
}
return "";
}
public static String tens(int num) {
switch(num) {
case 10: return "Ten";
case 11: return "Eleven";
case 12: return "Twelve";
case 13: return "Thirteen";
case 14: return "Fourteen";
case 15: return "Fifteen";
case 16: return "Sixteen";
case 17: return "Seventeen";
case 18: return "Eighteen";
case 19: return "Nineteen";
}
return "";
}
public static String ones(int num) {
String prefix = "";
if (num == 1) {
prefix = "One";
} else if (num == 2) {
prefix = "Two";
} else if (num == 3) {
prefix = "Three";
} else if (num == 4) {
prefix = "Four";
} else if (num == 5) {
prefix = "Five";
} else if (num == 6) {
prefix = "Six";
} else if (num == 7) {
prefix = "Seven";
} else if (num == 8) {
prefix = "Eight";
} else if (num == 9) {
prefix = "Nine";
} else if (num == 0) {
prefix = "Zero";
}
return prefix;
}
}