Created
September 9, 2024 14:51
-
-
Save LogicSatinn/550cbda91667af6666e33b37940e80a7 to your computer and use it in GitHub Desktop.
Convert Monetary Amount to Swahili Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| import java.lang.*; | |
| import java.io.*; | |
| import java.text.DecimalFormat; | |
| class NumberToSwahiliConverter { | |
| private static final String[] scalesInSwahili = {"", "laki", "milioni", "bilioni"}; | |
| private static final String[] units = { | |
| "", "moja", "mbili", "tatu", "nne", "tano", "sita", "saba", "nane", "tisa" | |
| }; | |
| private static final String[] teens = { | |
| "kumi", "kumi na moja", "kumi na mbili", "kumi na tatu", "kumi na nne", "kumi na tano", | |
| "kumi na sita", "kumi na saba", "kumi na nane", "kumi na tisa" | |
| }; | |
| private static final String[] tens = { | |
| "", "kumi", "ishirini", "thelathini", "arobaini", "hamsini", "sitini", "sabini", "themanini", "tisini" | |
| }; | |
| public static String convert(long number) { | |
| if (number == 0) { | |
| return "sifuri".toUpperCase(); | |
| } | |
| // pad with "0" | |
| String mask = "000000000000"; | |
| DecimalFormat df = new DecimalFormat(mask); | |
| String maskedNumber = df.format(number); | |
| final var result = new StringBuilder(); | |
| LinkedList<int[]> partitionIndices = new LinkedList<>(); | |
| partitionIndices.add(new int[]{0, 3}); | |
| partitionIndices.add(new int[]{3, 6}); | |
| partitionIndices.add(new int[]{6, 9}); | |
| partitionIndices.add(new int[]{9, 12}); | |
| for (int i = 0; i < partitionIndices.size(); i++) { | |
| int start = partitionIndices.get(i)[0]; | |
| int end = partitionIndices.get(i)[1]; | |
| int partition = Integer.parseInt(maskedNumber.substring(start, end)); | |
| if (partition != 0) { | |
| String partInWords = i == partitionIndices.size() - 2 | |
| ? convertHundredThousands(partition) | |
| : convertHundreds(partition); | |
| String scale = i == partitionIndices.size() - 2 && (partition / 100) == 0 | |
| ? "" | |
| : scalesInSwahili[partitionIndices.size() - 1 - i]; | |
| if (!scale.isEmpty()) { | |
| if (partition == 1 && scale.equals("milioni")) { | |
| result.append("milioni moja "); | |
| } else { | |
| result.append(scale).append(" ").append(partInWords).append(" "); | |
| } | |
| } else { | |
| result.append(partInWords).append(" "); | |
| } | |
| // if (partition > 0 && i == partitionIndices.size() - 2) { | |
| // result.append(" na "); | |
| // } | |
| } | |
| } | |
| // remove extra spaces | |
| return result | |
| .toString() | |
| .replaceAll("^\\s+", "") | |
| .replaceAll("\\b\\s{2,}\\b", " ") | |
| .trim() | |
| .toUpperCase() | |
| .strip(); | |
| } | |
| private static String convertHundreds(int num) { | |
| StringBuilder result = new StringBuilder(); | |
| if (num >= 100) { | |
| int hundreds = num / 100; | |
| if (hundreds == 1) { | |
| result.append("mia ").append(" "); | |
| } else { | |
| result.append(" mia ").append(units[hundreds]).append(" "); | |
| } | |
| num %= 100; | |
| } | |
| if (num >= 20) { | |
| result.append(tens[num / 10]).append(" "); | |
| num %= 10; | |
| if (num > 0) { | |
| result.append("na ").append(units[num]).append(" "); | |
| } | |
| } else if (num >= 10) { | |
| result.append(teens[num - 10]).append(" "); | |
| } else if (num > 0) { | |
| result.append(units[num]).append(" "); | |
| } | |
| return result.toString().trim(); | |
| } | |
| private static String convertHundredThousands(int num) { | |
| StringBuilder result = new StringBuilder(); | |
| if (num >= 100) { | |
| int hundreds = num / 100; | |
| if (hundreds == 1) { | |
| result.append("moja "); | |
| } else { | |
| result.append(units[hundreds]).append(" "); | |
| } | |
| num %= 100; | |
| } | |
| if (num > 0) { | |
| result.append("elfu "); | |
| } | |
| if (num >= 20) { | |
| result.append(tens[num / 10]).append(" "); | |
| num %= 10; | |
| if (num > 0) { | |
| result.append("na ").append(units[num]).append(" "); | |
| } | |
| } else if (num >= 10) { | |
| result.append(teens[num - 10]).append(" "); | |
| } else if (num > 0) { | |
| result.append(units[num]).append(" "); | |
| } | |
| return result.toString().trim(); | |
| } | |
| public static void main(String[] args) { | |
| System.out.println("*** " + NumberToWordsConverter.convert(123456789)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(123456989)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(0)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(1000)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(10000)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(10001)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(11000)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(100000)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(1000000)); | |
| System.out.println("*** " + NumberToWordsConverter.convert(1000000000)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment