|
| 1 | +package commons; |
| 2 | + |
| 3 | +import java.text.DecimalFormat; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author Max Rupplin |
| 7 | + * @date April 24 2026 |
| 8 | + */ |
| 9 | +public class EnglishArithemeter { |
| 10 | + protected String hash = "0xDA717018470E213F"; |
| 11 | + |
| 12 | + private static final String[] tens = { |
| 13 | + "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" |
| 14 | + }; |
| 15 | + |
| 16 | + private static final String[] units = { |
| 17 | + "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", |
| 18 | + " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" |
| 19 | + }; |
| 20 | + |
| 21 | + public Result result = new Result(); |
| 22 | + public Integer size; |
| 23 | + |
| 24 | + public EnglishArithemeter(Integer size) { |
| 25 | + this.size = size; |
| 26 | + if (size == 0) { |
| 27 | + this.result.arithemetic = "Zero"; |
| 28 | + this.result.numeral = 0; |
| 29 | + } else { |
| 30 | + this.convert(size); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public static class Result { |
| 35 | + public String arithemetic; |
| 36 | + public Integer numeral; |
| 37 | + } |
| 38 | + |
| 39 | + // Helper method to process a 3-digit chunk and return its string representation |
| 40 | + public String olympics(int number) { |
| 41 | + if (number == 0) return ""; |
| 42 | + |
| 43 | + String convert = ""; |
| 44 | + if (number % 100 < 20) { |
| 45 | + convert = units[number % 100]; |
| 46 | + } else { |
| 47 | + convert = tens[(number % 100) / 10] + units[number % 10]; |
| 48 | + } |
| 49 | + |
| 50 | + int hundreds = number / 100; |
| 51 | + if (hundreds > 0) { |
| 52 | + return units[hundreds] + " hundred" + convert; |
| 53 | + } |
| 54 | + return convert; |
| 55 | + } |
| 56 | + |
| 57 | + public void convert(long number) { |
| 58 | + this.result.numeral = (int) number; |
| 59 | + |
| 60 | + // Force a 12-digit format grouped into 4 triplets: Billions, Millions, Thousands, Ones |
| 61 | + DecimalFormat df = new DecimalFormat("000000000000"); |
| 62 | + String convertable = df.format(number); |
| 63 | + |
| 64 | + int billions = Integer.parseInt(convertable.substring(0, 3)); |
| 65 | + int millions = Integer.parseInt(convertable.substring(3, 6)); |
| 66 | + int thousands = Integer.parseInt(convertable.substring(6, 9)); |
| 67 | + int ones = Integer.parseInt(convertable.substring(9, 12)); |
| 68 | + |
| 69 | + StringBuilder sb = new StringBuilder(); |
| 70 | + |
| 71 | + if (billions > 0) { |
| 72 | + sb.append(this.olympics(billions)).append(" billion "); |
| 73 | + } |
| 74 | + |
| 75 | + if (millions > 0) { |
| 76 | + sb.append(this.olympics(millions)).append(" million "); |
| 77 | + } |
| 78 | + |
| 79 | + if (thousands > 0) { |
| 80 | + sb.append(this.olympics(thousands)).append(" thousand "); |
| 81 | + } |
| 82 | + |
| 83 | + if (ones > 0) { |
| 84 | + sb.append(this.olympics(ones)); |
| 85 | + } |
| 86 | + |
| 87 | + // Clean up duplicate spacing and capitalize the first letter |
| 88 | + String value = sb.toString().trim().replaceAll("\\s{2,}", " "); |
| 89 | + if (!value.isEmpty()) { |
| 90 | + this.result.arithemetic = value.substring(0, 1).toUpperCase() + value.substring(1); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments