Skip to content

Commit 59dd1dc

Browse files
committed
refactor: Enhance docs, add more tests in IntegerToEnglish
1 parent e499d3b commit 59dd1dc

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@
22

33
import java.util.Map;
44

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+
*/
525
public final class IntegerToEnglish {
26+
627
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"),
728
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"),
829
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,11 +34,13 @@ private IntegerToEnglish() {
1334
}
1435

1536
/**
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
1741
*/
1842
private static String convertToWords(int number) {
1943
int remainder = number % 100;
20-
2144
String result;
2245

2346
if (remainder <= 20) {
@@ -27,29 +50,29 @@ private static String convertToWords(int number) {
2750
} else {
2851
int tensDigit = remainder / 10;
2952
int onesDigit = remainder % 10;
30-
31-
result = String.format("%s %s", BASE_NUMBERS_MAP.get(tensDigit * 10), BASE_NUMBERS_MAP.get(onesDigit));
53+
result = String.format("%s %s", BASE_NUMBERS_MAP.get(tensDigit * 10), BASE_NUMBERS_MAP.get(onesDigit)).trim();
3254
}
3355

3456
int hundredsDigit = number / 100;
35-
3657
if (hundredsDigit > 0) {
37-
result = String.format("%s %s%s", BASE_NUMBERS_MAP.get(hundredsDigit), BASE_NUMBERS_MAP.get(100), result.isEmpty() ? "" : " " + result);
58+
result = String.format("%s Hundred%s%s", BASE_NUMBERS_MAP.get(hundredsDigit), result.isEmpty() ? "" : " ", result);
3859
}
3960

40-
return result;
61+
return result.trim();
4162
}
4263

4364
/**
44-
Only convert groups of three digit if they are non-zero
65+
* Converts a non-negative integer to its English word representation.
66+
*
67+
* @param number the integer to convert (0-2,147,483,647)
68+
* @return the English word representation of the input number
4569
*/
4670
public static String integerToEnglishWords(int number) {
4771
if (number == 0) {
4872
return "Zero";
4973
}
5074

5175
StringBuilder result = new StringBuilder();
52-
5376
int index = 0;
5477

5578
while (number > 0) {
@@ -58,23 +81,21 @@ public static String integerToEnglishWords(int number) {
5881

5982
if (remainder > 0) {
6083
String subResult = convertToWords(remainder);
61-
6284
if (!subResult.isEmpty()) {
85+
if (index > 0) {
86+
subResult += " " + THOUSAND_POWER_MAP.get(index);
87+
}
6388
if (!result.isEmpty()) {
64-
result.insert(0, subResult + " " + THOUSAND_POWER_MAP.get(index) + " ");
89+
result.insert(0, subResult + " ");
6590
} else {
66-
if (index > 0) {
67-
result = new StringBuilder(subResult + " " + THOUSAND_POWER_MAP.get(index));
68-
} else {
69-
result = new StringBuilder(subResult);
70-
}
91+
result.append(subResult);
7192
}
7293
}
7394
}
7495

7596
index++;
7697
}
7798

78-
return result.toString();
99+
return result.toString().trim();
79100
}
80101
}

src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,36 @@ public void testIntegerToEnglish() {
1111
assertEquals("Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven", IntegerToEnglish.integerToEnglishWords(2147483647));
1212
assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", IntegerToEnglish.integerToEnglishWords(1234567));
1313
assertEquals("Twelve Thousand Three Hundred Forty Five", IntegerToEnglish.integerToEnglishWords(12345));
14+
assertEquals("One Hundred", IntegerToEnglish.integerToEnglishWords(100));
15+
assertEquals("Zero", IntegerToEnglish.integerToEnglishWords(0));
16+
}
17+
18+
@Test
19+
public void testSmallNumbers() {
20+
assertEquals("Ten", IntegerToEnglish.integerToEnglishWords(10));
21+
assertEquals("Nineteen", IntegerToEnglish.integerToEnglishWords(19));
22+
assertEquals("Twenty One", IntegerToEnglish.integerToEnglishWords(21));
23+
assertEquals("Ninety Nine", IntegerToEnglish.integerToEnglishWords(99));
24+
}
25+
26+
@Test
27+
public void testHundreds() {
28+
assertEquals("One Hundred One", IntegerToEnglish.integerToEnglishWords(101));
29+
assertEquals("Five Hundred Fifty", IntegerToEnglish.integerToEnglishWords(550));
30+
assertEquals("Nine Hundred Ninety Nine", IntegerToEnglish.integerToEnglishWords(999));
31+
}
32+
33+
@Test
34+
public void testThousands() {
35+
assertEquals("One Thousand", IntegerToEnglish.integerToEnglishWords(1000));
36+
assertEquals("Ten Thousand One", IntegerToEnglish.integerToEnglishWords(10001));
37+
assertEquals("Seventy Six Thousand Five Hundred Forty Three", IntegerToEnglish.integerToEnglishWords(76543));
38+
}
39+
40+
@Test
41+
public void testEdgeCases() {
42+
assertEquals("One Million", IntegerToEnglish.integerToEnglishWords(1_000_000));
43+
assertEquals("One Billion", IntegerToEnglish.integerToEnglishWords(1_000_000_000));
44+
assertEquals("Two Thousand", IntegerToEnglish.integerToEnglishWords(2000));
1445
}
1546
}

0 commit comments

Comments
 (0)