Converting a number to words in Java can be quite complex, especially if you need to handle large numbers or different formats. Here's an implementation that can convert numbers up to billions into words. This example handles English language conversion.
Example Code
java
public class NumberToWords {
private static final String[] BELOW_TWENTY = {
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
};
private static final String[] TENS = {
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
private static final String[] THOUSANDS = {
"", "Thousand", "Million", "Billion"
};
public static void main(String[] args) {
System.out.println(numberToWords(123)); // One Hundred Twenty Three
System.out.println(numberToWords(12345)); // Twelve Thousand Three Hundred Forty Five
System.out.println(numberToWords(1234567)); // One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven
System.out.println(numberToWords(1234567891)); // One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One
}
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 words.trim();
}
private static String helper(int num) {
if (num == 0)
return "";
else if (num < 20)
return BELOW_TWENTY[num] + " ";
else if (num < 100)
return TENS[num / 10] + " " + helper(num % 10);
else
return BELOW_TWENTY[num / 100] + " Hundred " + helper(num % 100);
}
}
Explanation
- Constants
Arrays:
- BELOW_TWENTY: Contains
words for numbers from 0 to 19.
- TENS: Contains words for
multiples of ten from 20 to 90.
- THOUSANDS: Contains the
units for thousands, millions, and billions.
- numberToWords
Method:
- This is the main method
that converts an integer to words.
- It checks if the number
is zero and returns "Zero".
- It iterates through the
number in chunks of three digits (thousands) and builds the word
representation by calling the helper method.
- It keeps track of the
thousands place using the THOUSANDS array.
- helper
Method:
- This method handles
numbers less than 1000 and converts them to words.
- It recursively processes
the number, handling hundreds, tens, and units separately.
Example Usage
Ø numberToWords(123) returns "One Hundred Twenty Three".
Ø numberToWords(12345) returns "Twelve Thousand Three Hundred Forty Five".
Ø numberToWords(1234567) returns "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven".
Ø numberToWords(1234567891) returns "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One".
- Constants
Arrays:
- Home-icon
- DSA
- _Sorting
- __Bubble Sort
- __Selection Sort
- __Insertion Sort
- __Merge Sort
- __Quick Sort
- __Simple Merge Sort
- _Algorithm
- __Multiple Attribute Sort
- __Sort 10 billion numbers
- __Case sensitive sort
- __Different Way Multiplication
- __Different Way Division
- __Karatsubas Algo
- __Date palindrome
- __Binary Tree
- __Two Jar difference
- __100 doors
- __Tiger Goat
- __Find Itinerary
- _Java
- __jdk 1.8 lambdas
- __jdk 1.8 static
- __Serilization
- __Threadpool Execcutor
- __Jvm compiler
- __Print number
- __Multitasking
- __Garbage collection
- __Exception
- __Mutable
- __Break and Continue
- __Clone
- __Enum
- __Inheritance
- _Custom/Generic DS
- __Custom HashMap
- __Custom Stack
- __Custom Doubly Linklist
- __Custom Singlly Linklist
- __Custom Array List
- __Stack Reverse Order
- __Custom Collection Reverse Stack
- _Custom Masking
- __Masking String
- _Miscellaneous
- __Copy Properties
- __Seat Arrangement Sort
- __Number to Word
- __Pascal Triangle
- __Permutaion and Combination
- __Custom Permutaion and Combination
- __Excel file column number
- __Convert Degree to radian
- __Random number Generate
- __Alphanumeric random number
- __File monitoring
- __Jasper report
- __Multiple file read
- Chemical
- _Chemical look
- _Molecular weight
- _Orbitals to electron images using Hund's rule
- Tools
- _Sql
- __Sql query
- __DB lock
- __floating-double
- _NoSql
- __Mongodb install
- __Mongodb load balancer or replica
- __Mongodb shard setup
- __Mongodb connection
- _Stylus Studio
- __Stylus Studio EDI MSG
- __Edifact Message Conveter
- _Miscellaneous Tools
- __Database Rever Engine
- __Hot Swap Tool
- __Hot Deployment Tool
- __Visualvm Monitoring Setup
- __Jconsole Monitoring Setup
- __Postman Setup
- __Remote debug
- __Git Command
- __Apache loadblancer
- Security
- _Encryption
- __Hashmac Algo
- __generating-keystore-files
- __Symetric
- __Asymmetric
- __Asymmetric using 3ds
- __OWASP Top 10
- __HTTP and HTTPS
- _TLS
- __TLS 1.2
- __TLS 1.3 Configuration
- __TLS 1.3 browsers
- _Oauth
- __Oauth 2.0
- __Oauth 2.0 program
- Spring
- _Spring Info
- __Filter
- __Debounce,Throttling, RateLimit and BackOff
- _Microservice
- __Filter
- __Custom Filter
- _SCG
- __rewritepath
- Linux
- _Command
- _Jenkins Pipeline
- _Deployment process
- Design
- _Generic
- __Time Complexity
- __Java Design
- __Master Design Pattern
- __Leaky Bucket Algorithm
- _Cloud
- __Google Cloud Services
- __Cloud Application Tier
- __Cloud Hierarchical
- __Orika mapper
- __Sequence Diagram
0 Comments