2
2
3
3
import java .util .Map ;
4
4
5
+ /**
6
+ * A utility class to convert integers to their English word representation.
7
+ *
8
+ * <p>The class supports conversion of numbers from 0 to 2,147,483,647
9
+ * (the maximum value of a 32-bit signed integer). It divides the number
10
+ * into groups of three digits (thousands, millions, billions, etc.) and
11
+ * translates each group into words.</p>
12
+ *
13
+ * <h2>Example Usage</h2>
14
+ * <pre>
15
+ * IntegerToEnglish.integerToEnglishWords(12345);
16
+ * // Output: "Twelve Thousand Three Hundred Forty Five"
17
+ * </pre>
18
+ *
19
+ * <p>This class uses two maps:</p>
20
+ * <ul>
21
+ * <li>BASE_NUMBERS_MAP: Holds English words for numbers 0-20, multiples of 10 up to 90, and 100.</li>
22
+ * <li>THOUSAND_POWER_MAP: Maps powers of 1000 (e.g., Thousand, Million, Billion).</li>
23
+ * </ul>
24
+ */
5
25
public final class IntegerToEnglish {
26
+
6
27
private static final Map <Integer , String > BASE_NUMBERS_MAP = Map .ofEntries (Map .entry (0 , "" ), Map .entry (1 , "One" ), Map .entry (2 , "Two" ), Map .entry (3 , "Three" ), Map .entry (4 , "Four" ), Map .entry (5 , "Five" ), Map .entry (6 , "Six" ), Map .entry (7 , "Seven" ), Map .entry (8 , "Eight" ), Map .entry (9 , "Nine" ),
7
28
Map .entry (10 , "Ten" ), Map .entry (11 , "Eleven" ), Map .entry (12 , "Twelve" ), Map .entry (13 , "Thirteen" ), Map .entry (14 , "Fourteen" ), Map .entry (15 , "Fifteen" ), Map .entry (16 , "Sixteen" ), Map .entry (17 , "Seventeen" ), Map .entry (18 , "Eighteen" ), Map .entry (19 , "Nineteen" ), Map .entry (20 , "Twenty" ),
8
29
Map .entry (30 , "Thirty" ), Map .entry (40 , "Forty" ), Map .entry (50 , "Fifty" ), Map .entry (60 , "Sixty" ), Map .entry (70 , "Seventy" ), Map .entry (80 , "Eighty" ), Map .entry (90 , "Ninety" ), Map .entry (100 , "Hundred" ));
@@ -13,43 +34,53 @@ private IntegerToEnglish() {
13
34
}
14
35
15
36
/**
16
- converts numbers < 1000 to english words
37
+ * Converts numbers less than 1000 into English words.
38
+ *
39
+ * @param number the integer value (0-999) to convert
40
+ * @return the English word representation of the input number
17
41
*/
18
42
private static String convertToWords (int number ) {
19
43
int remainder = number % 100 ;
20
-
21
- String result ;
44
+ StringBuilder result = new StringBuilder ();
22
45
23
46
if (remainder <= 20 ) {
24
- result = BASE_NUMBERS_MAP .get (remainder );
47
+ result . append ( BASE_NUMBERS_MAP .get (remainder ) );
25
48
} else if (BASE_NUMBERS_MAP .containsKey (remainder )) {
26
- result = BASE_NUMBERS_MAP .get (remainder );
49
+ result . append ( BASE_NUMBERS_MAP .get (remainder ) );
27
50
} else {
28
51
int tensDigit = remainder / 10 ;
29
52
int onesDigit = remainder % 10 ;
30
-
31
- result = String .format ("%s %s" , BASE_NUMBERS_MAP .get (tensDigit * 10 ), BASE_NUMBERS_MAP .get (onesDigit ));
53
+ String tens = BASE_NUMBERS_MAP .getOrDefault (tensDigit * 10 , "" );
54
+ String ones = BASE_NUMBERS_MAP .getOrDefault (onesDigit , "" );
55
+ result .append (tens );
56
+ if (ones != null && !ones .isEmpty ()) {
57
+ result .append (" " ).append (ones );
58
+ }
32
59
}
33
60
34
61
int hundredsDigit = number / 100 ;
35
-
36
62
if (hundredsDigit > 0 ) {
37
- result = String .format ("%s %s%s" , BASE_NUMBERS_MAP .get (hundredsDigit ), BASE_NUMBERS_MAP .get (100 ), result .isEmpty () ? "" : " " + result );
63
+ if (result .length () > 0 ) {
64
+ result .insert (0 , " " );
65
+ }
66
+ result .insert (0 , String .format ("%s Hundred" , BASE_NUMBERS_MAP .get (hundredsDigit )));
38
67
}
39
68
40
- return result ;
69
+ return result . toString (). trim () ;
41
70
}
42
71
43
72
/**
44
- Only convert groups of three digit if they are non-zero
73
+ * Converts a non-negative integer to its English word representation.
74
+ *
75
+ * @param number the integer to convert (0-2,147,483,647)
76
+ * @return the English word representation of the input number
45
77
*/
46
78
public static String integerToEnglishWords (int number ) {
47
79
if (number == 0 ) {
48
80
return "Zero" ;
49
81
}
50
82
51
83
StringBuilder result = new StringBuilder ();
52
-
53
84
int index = 0 ;
54
85
55
86
while (number > 0 ) {
@@ -58,23 +89,20 @@ public static String integerToEnglishWords(int number) {
58
89
59
90
if (remainder > 0 ) {
60
91
String subResult = convertToWords (remainder );
61
-
62
92
if (!subResult .isEmpty ()) {
63
- if (!result .isEmpty ()) {
64
- result .insert (0 , subResult + " " + THOUSAND_POWER_MAP .get (index ) + " " );
65
- } else {
66
- if (index > 0 ) {
67
- result = new StringBuilder (subResult + " " + THOUSAND_POWER_MAP .get (index ));
68
- } else {
69
- result = new StringBuilder (subResult );
70
- }
93
+ if (index > 0 ) {
94
+ subResult += " " + THOUSAND_POWER_MAP .get (index );
95
+ }
96
+ if (result .length () > 0 ) {
97
+ result .insert (0 , " " );
71
98
}
99
+ result .insert (0 , subResult );
72
100
}
73
101
}
74
102
75
103
index ++;
76
104
}
77
105
78
- return result .toString ();
106
+ return result .toString (). trim () ;
79
107
}
80
108
}
0 commit comments