Repository: TheAlgorithms/Java
Pull request #5540 (2024-10-03 06:57:46)

feat: add conversion logic from integer to english
Add Conversion logic to convert Integer range numbers to English Locale.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5540?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `86.36364%` with `9 lines` in your changes missing coverage. Please review.
> Project coverage is 52.95%. Comparing base [(`ea0ed23`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ea0ed2396114133b1396c443551503fef7fedb4d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`ce3649e`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ce3649eb3f1f5e02ece9d223924d4e80c09b5446?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5540?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...om/thealgorithms/conversions/IntegerToEnglish.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5540?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fconversions%2FIntegerToEnglish.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jb252ZXJzaW9ucy9JbnRlZ2VyVG9FbmdsaXNoLmphdmE=) | 86.36% | [3 Missing and 6 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5540?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5540      +/-   ##
============================================
+ Coverage     52.80%   52.95%   +0.15%     
- Complexity     3305     3311       +6     
============================================
  Files           528      529       +1     
  Lines         15354    15420      +66     
  Branches       2924     2933       +9     
============================================
+ Hits           8108     8166      +58     
- Misses         6912     6914       +2     
- Partials        334      340       +6     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5540?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 3662a8cd..0b282d8f 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -68,6 +68,7 @@
             * [HexaDecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java)
             * [HexaDecimalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexaDecimalToDecimal.java)
             * [HexToOct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/HexToOct.java)
+            * [IntegerToEnglish] (https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java)
             * [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java)
             * [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
             * [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
diff --git a/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java b/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java
new file mode 100644
index 00000000..d3b938bf
--- /dev/null
+++ b/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java
@@ -0,0 +1,80 @@
+package com.thealgorithms.conversions;
+
+import java.util.Map;
+
+public final class IntegerToEnglish {
+    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"),
+        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"),
+        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"));
+
+    private static final Map<Integer, String> THOUSAND_POWER_MAP = Map.ofEntries(Map.entry(1, "Thousand"), Map.entry(2, "Million"), Map.entry(3, "Billion"));
+
+    private IntegerToEnglish() {
+    }
+
+    /**
+        converts numbers < 1000 to english words
+     */
+    private static String convertToWords(int number) {
+        int remainder = number % 100;
+
+        String result;
+
+        if (remainder <= 20) {
+            result = BASE_NUMBERS_MAP.get(remainder);
+        } else if (BASE_NUMBERS_MAP.containsKey(remainder)) {
+            result = BASE_NUMBERS_MAP.get(remainder);
+        } else {
+            int tensDigit = remainder / 10;
+            int onesDigit = remainder % 10;
+
+            result = String.format("%s %s", BASE_NUMBERS_MAP.get(tensDigit * 10), BASE_NUMBERS_MAP.get(onesDigit));
+        }
+
+        int hundredsDigit = number / 100;
+
+        if (hundredsDigit > 0) {
+            result = String.format("%s %s%s", BASE_NUMBERS_MAP.get(hundredsDigit), BASE_NUMBERS_MAP.get(100), result.isEmpty() ? "" : " " + result);
+        }
+
+        return result;
+    }
+
+    /**
+      Only convert groups of three digit if they are non-zero
+     */
+    public static String integerToEnglishWords(int number) {
+        if (number == 0) {
+            return "Zero";
+        }
+
+        StringBuilder result = new StringBuilder();
+
+        int index = 0;
+
+        while (number > 0) {
+            int remainder = number % 1000;
+            number /= 1000;
+
+            if (remainder > 0) {
+                String subResult = convertToWords(remainder);
+
+                if (!subResult.isEmpty()) {
+                    if (!result.isEmpty()) {
+                        result.insert(0, subResult + " " + THOUSAND_POWER_MAP.get(index) + " ");
+                    } else {
+                        if (index > 0) {
+                            result = new StringBuilder(subResult + " " + THOUSAND_POWER_MAP.get(index));
+                        } else {
+                            result = new StringBuilder(subResult);
+                        }
+                    }
+                }
+            }
+
+            index++;
+        }
+
+        return result.toString();
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java b/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java
new file mode 100644
index 00000000..49c43402
--- /dev/null
+++ b/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java
@@ -0,0 +1,15 @@
+package com.thealgorithms.conversions;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class IntegerToEnglishTest {
+
+    @Test
+    public void testIntegerToEnglish() {
+        assertEquals("Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven", IntegerToEnglish.integerToEnglishWords(2147483647));
+        assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", IntegerToEnglish.integerToEnglishWords(1234567));
+        assertEquals("Twelve Thousand Three Hundred Forty Five", IntegerToEnglish.integerToEnglishWords(12345));
+    }
+}