1. Requirements Analysis
Suppose we need a method, enter a double type value (representing the amount) and output its corresponding English expression. For example, input 123456.78, the output should be One Hundred Twenty Three Thousand Four Hundred Fifty Six Dollars and Seventy Eight Cents.
2. Technical selection
- programming language:Java
- Development Tools:IntelliJ IDEA
- Test Tools:JUnit
3. Implementation ideas
3.1 Decompose the numbers
First, break the number into integer and decimal parts. For integer parts, grouped by thousands, and each group is converted into English separately; for decimal parts, it is converted into English directly.
3.2 Number to English mapping
Create two arrays, storing English representations of 0-19 and 20-90 respectively, and a string array stores English representations of 1,000,000, and millions of units.
3.3 Transformation logic
- For numbers less than 20, get the English representation directly from the array.
- For numbers between 20 and 99, ten bits are processed first, and then single bits are processed.
- For numbers above 100, the parts of hundreds or more are processed recursively.
4. Code implementation
public class NumberToWordsConverter { private static final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private static final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private static final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) { words = helper(num % 1000) + THOUSANDS[i] + " " + words; } num /= 1000; i++; } return (); } private String helper(int num) { if (num == 0) return ""; else if (num < 20) return LESS_THAN_20[num] + " "; else if (num < 100) return TENS[num / 10] + " " + helper(num % 10); else return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100); } public String convert(double amount) { long integerPart = (long) amount; double decimalPart = amount - integerPart; String integerWords = numberToWords((int) integerPart); String decimalWords = numberToWords((int) (decimalPart * 100)); return integerWords + " Dollars and " + decimalWords + " Cents"; } }
5. Test
To ensure that our conversion function is correct, some unit tests can be written to verify:
import ; import static ; public class NumberToWordsConverterTest { @Test public void testConvert() { NumberToWordsConverter converter = new NumberToWordsConverter(); assertEquals("One Hundred Twenty Three Thousand Four Hundred Fifty Six Dollars and Seventy Eight Cents", (123456.78)); assertEquals("One Dollar and Twenty Three Cents", (1.23)); assertEquals("Zero Dollars and Zero Cents", (0.00)); } }
We successfully implemented the function of converting the digital amount into English description. This feature is very useful in practical applications, especially when dealing with international financial systems. In applications, the need to convert digital amounts to English amounts is common in banking systems, financial software and other scenarios. This feature is often used to generate formal financial documents such as checks, invoices, etc. to ensure the accuracy and tamper-proof of the amount.
Here is a simple Java sample code showing how to convert a numeric amount (e.g. 123456.78) to an English amount (e.g. "One Hundred Twenty Three Thousand Four Hundred Fifty Six Dollars and Seventy Eight Cents". This example uses basic string processing and array mapping to implement transformation logic.
import ; import ; public class NumberToWordsConverter { private static final String[] unitsMap = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; private static final String[] tensMap = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; private static final Map<Long, String> thousandsMap = new HashMap<>(); static { (1000000000L, "Billion"); (1000000L, "Million"); (1000L, "Thousand"); (1L, ""); } public static void main(String[] args) { double amount = 123456.78; ("Amount in words: " + convertNumberToWords(amount)); } public static String convertNumberToWords(double number) { long integerPart = (long) number; int decimalPart = (int) ((number - integerPart) * 100); StringBuilder result = new StringBuilder(); if (integerPart == 0) { ("Zero Dollars"); } else { (convertLessThanOneThousand(integerPart)).append(" Dollars"); } if (decimalPart > 0) { (" and ").append(convertLessThanOneThousand(decimalPart)).append(" Cents"); } return ().trim(); } private static String convertLessThanOneThousand(long number) { if (number % 100 < 20) { return unitsMap[(int) number % 100]; } else { return tensMap[(int) (number % 100 / 10)] + " " + unitsMap[(int) (number % 10)]; } } private static String convert(long number) { if (number == 0) { return ""; } for (<Long, String> entry : ()) { if (number >= ()) { return convert(number / ()) + " " + () + " " + convert(number % ()); } } return ""; } }
Code explanation:
- unitsMapandtensMap: These two arrays store single-digit and ten-digit English representations respectively.
- thousandsMap: This is a hash table used to store units of more than 1,000 digits (such as Thousand, Million, Billion).
- convertNumberToWords: This is the main function, responsible for converting the numeric amount into English representation. It first processes the integer part and then the decimal part.
- convertLessThanOneThousand: This helper function is used to convert numbers less than 1000 into English.
- convert: This recursive function is used to process numbers greater than 1000 and is divided according to thousand units.
With the above code, you can convert any valid numeric amount into its corresponding English representation. Converting digital amounts in Java to English amounts is a common requirement, especially in scenarios where financial reports, invoices, etc. need to accurately represent the amount. Below I will introduce in detail how to implement this function in Java.
Implementation ideas
- Split integer part and decimal part: First, split the entered amount string into integer and decimal parts according to the decimal point.
- Define the mapping of numbers and units: Create a mapping to map numbers 0-19 and special units such as 100, 1000, 1000, 1000000 to English words.
- Processing integer parts: Recursively convert integer parts into English words.
- Process the fractional part: Convert two numbers in the decimal part directly into English words.
- Combination results: Combine the English words of the integer part and the decimal part to form the final English amount representation.
Code implementation
import ; import ; public class NumberToWordsConverter { private static final String[] LESS_THAN_20 = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; private static final String[] TENS = { "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; private static final String[] THOUSANDS = { "", "Thousand", "Million", "Billion" }; public static String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { if (num % 1000 != 0) { words = helper(num % 1000) + THOUSANDS[i] + " " + words; } num /= 1000; i++; } return (); } private static String helper(int num) { if (num == 0) { return ""; } else if (num < 20) { return LESS_THAN_20[num] + " "; } else if (num < 100) { return TENS[num / 10] + " " + helper(num % 10); } else { return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100); } } public static String convertAmountToWords(double amount) { long integerPart = (long) amount; int decimalPart = (int) ((amount - integerPart) * 100); String integerWords = numberToWords((int) integerPart); String decimalWords = numberToWords(decimalPart); return () + " Dollars and " + () + " Cents"; } public static void main(String[] args) { double amount = 1234.56; (convertAmountToWords(amount)); } }
Code explanation
- Constant definition:
-
LESS_THAN_20
: Contains English words from 0 to 19. -
TENS
: Contains English words from 10 to 90. -
THOUSANDS
: English words containing units of thousands, millions, billions, etc.
-
numberToWords
Methods:
- Convert integer parts to English words. Recursively process each three-digit part and add the corresponding units (thousands, millions, billions).
-
helper
Methods:
- Process numbers less than 1000 and convert them into English words.
-
convertAmountToWords
Methods:
- Split the entered amount into integer and decimal parts.
- Call
numberToWords
Methods convert integer and decimal parts into English words respectively. - Combining the results to form the final English amount representation.
-
main
Methods:
- Test example, convert 1234.56 to English amount representation.
Output result
Run the above code and the output is:
One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents
This implementation can handle most common amount conversion requirements. If larger amounts or more complex units are needed, they can be further expanded and optimized.
This is the article about Java's ability to convert digital amounts into English amounts. For more relevant content on Java's digital amounts to English amounts, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!