Skip to content

Commit ca12025

Browse files
author
sailok.chinta
committed
feat: add conversion logic from integer to english
1 parent d436910 commit ca12025

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.HashMap;
4+
5+
public class IntegerToEnglish {
6+
private static final HashMap<Integer, String> baseNumbersMap = new HashMap<>() {
7+
{
8+
put(0, "");
9+
put(1, "One");
10+
put(2, "Two");
11+
put(3, "Three");
12+
put(4, "Four");
13+
put(5, "Five");
14+
put(6, "Six");
15+
put(7, "Seven");
16+
put(8, "Eight");
17+
put(9, "Nine");
18+
put(10, "Ten");
19+
put(11, "Eleven");
20+
put(12, "Twelve");
21+
put(13, "Thirteen");
22+
put(14, "Fourteen");
23+
put(15, "Fifteen");
24+
put(16, "Sixteen");
25+
put(17, "Seventeen");
26+
put(18, "Eighteen");
27+
put(19, "Nineteen");
28+
put(20, "Twenty");
29+
put(30, "Thirty");
30+
put(40, "Forty");
31+
put(50, "Fifty");
32+
put(60, "Sixty");
33+
put(70, "Seventy");
34+
put(80, "Eighty");
35+
put(90, "Ninety");
36+
put(100, "Hundred");
37+
}
38+
};
39+
40+
private static final HashMap<Integer, String> thousandPowerMap = new HashMap<>() {
41+
{
42+
put(1, "Thousand");
43+
put(2, "Million");
44+
put(3, "Billion");
45+
}
46+
};
47+
48+
/**
49+
converts numbers < 1000 to english words
50+
*/
51+
private static String convertToWords(int number) {
52+
int remainder = number % 100;
53+
54+
String result;
55+
56+
if (remainder <= 20) {
57+
result = baseNumbersMap.get(remainder);
58+
} else if (baseNumbersMap.containsKey(remainder)) {
59+
result = baseNumbersMap.get(remainder);
60+
} else {
61+
int tensDigit = remainder / 10;
62+
int onesDigit = remainder % 10;
63+
64+
result = String.format("%s %s", baseNumbersMap.get(tensDigit * 10), baseNumbersMap.get(onesDigit));
65+
}
66+
67+
int hundredsDigit = number / 100;
68+
69+
if (hundredsDigit > 0) {
70+
result = String.format("%s %s%s", baseNumbersMap.get(hundredsDigit), baseNumbersMap.get(100), (result.isEmpty() ? "" : " " + result));
71+
}
72+
73+
return result;
74+
}
75+
76+
/**
77+
Only convert groups of three digit if they are non-zero
78+
*/
79+
public static String integerToEnglishWords(int number) {
80+
if (number == 0) return "Zero";
81+
82+
StringBuilder result = new StringBuilder();
83+
84+
int index = 0;
85+
86+
while (number > 0) {
87+
int remainder = number % 1000;
88+
number /= 1000;
89+
90+
if (remainder > 0) {
91+
String subResult = convertToWords(remainder);
92+
93+
if (!subResult.isEmpty()) {
94+
if (!result.isEmpty()) {
95+
result.insert(0, subResult + " " + thousandPowerMap.get(index) + " ");
96+
} else {
97+
if (index > 0) {
98+
result = new StringBuilder(subResult + " " + thousandPowerMap.get(index));
99+
} else {
100+
result = new StringBuilder(subResult);
101+
}
102+
}
103+
}
104+
}
105+
106+
index++;
107+
}
108+
109+
return result.toString();
110+
}
111+
}
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)