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