Skip to content

Commit 389d1d7

Browse files
sailoksailok.chintaalxkm
authored
feat: add conversion logic from integer to english (#5540)
* feat: add conversion logic from integer to english * feat: update DIRECTORY.md * feat: fix linting issues * feat: fix build issue * feat: address review comments --------- Co-authored-by: sailok.chinta <[email protected]> Co-authored-by: Alex Klymenko <[email protected]>
1 parent ea6457b commit 389d1d7

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
* [HexaDecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java)
6969
* [HexaDecimalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java)
7070
* [HexToOct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexToOct.java)
71+
* [IntegerToEnglish] (https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java)
7172
* [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java)
7273
* [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
7374
* [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.Map;
4+
5+
public final class IntegerToEnglish {
6+
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+
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+
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"));
9+
10+
private static final Map<Integer, String> THOUSAND_POWER_MAP = Map.ofEntries(Map.entry(1, "Thousand"), Map.entry(2, "Million"), Map.entry(3, "Billion"));
11+
12+
private IntegerToEnglish() {
13+
}
14+
15+
/**
16+
converts numbers < 1000 to english words
17+
*/
18+
private static String convertToWords(int number) {
19+
int remainder = number % 100;
20+
21+
String result;
22+
23+
if (remainder <= 20) {
24+
result = BASE_NUMBERS_MAP.get(remainder);
25+
} else if (BASE_NUMBERS_MAP.containsKey(remainder)) {
26+
result = BASE_NUMBERS_MAP.get(remainder);
27+
} else {
28+
int tensDigit = remainder / 10;
29+
int onesDigit = remainder % 10;
30+
31+
result = String.format("%s %s", BASE_NUMBERS_MAP.get(tensDigit * 10), BASE_NUMBERS_MAP.get(onesDigit));
32+
}
33+
34+
int hundredsDigit = number / 100;
35+
36+
if (hundredsDigit > 0) {
37+
result = String.format("%s %s%s", BASE_NUMBERS_MAP.get(hundredsDigit), BASE_NUMBERS_MAP.get(100), result.isEmpty() ? "" : " " + result);
38+
}
39+
40+
return result;
41+
}
42+
43+
/**
44+
Only convert groups of three digit if they are non-zero
45+
*/
46+
public static String integerToEnglishWords(int number) {
47+
if (number == 0) {
48+
return "Zero";
49+
}
50+
51+
StringBuilder result = new StringBuilder();
52+
53+
int index = 0;
54+
55+
while (number > 0) {
56+
int remainder = number % 1000;
57+
number /= 1000;
58+
59+
if (remainder > 0) {
60+
String subResult = convertToWords(remainder);
61+
62+
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+
}
71+
}
72+
}
73+
}
74+
75+
index++;
76+
}
77+
78+
return result.toString();
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class IntegerToEnglishTest {
8+
9+
@Test
10+
public void testIntegerToEnglish() {
11+
assertEquals("Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven", IntegerToEnglish.integerToEnglishWords(2147483647));
12+
assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", IntegerToEnglish.integerToEnglishWords(1234567));
13+
assertEquals("Twelve Thousand Three Hundred Forty Five", IntegerToEnglish.integerToEnglishWords(12345));
14+
}
15+
}

0 commit comments

Comments
 (0)