|
| 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 | +} |
0 commit comments