In this post we are going to learn How to convert number to words in java.Here we will read a number from the console and converts the number to its word form. Example, if the number entered is 65, the program should print sixty five. For input 9535, it should print nine thousand five hundred thirty five and so on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials *This java program is all about How to convert an integer number to equivalent in word format */ public class NumberToWordConverter { public static void main(String[] args) { int number = 0; try(Scanner scanner = new Scanner(System.in)) { //read number from keyboard System.out.println("Please Enter a number(max upto 9 digits):"); number = scanner.nextInt(); System.out.print("Number in words: " + convertNumberToWords(number)); } catch (Exception e) { System.out.println("Please enter a valid number"); } } private static String convertNumberToWords(int number) { //this variable is going to hold string representation of number String words = ""; String unitsArray[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; String tensArray[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; if (number == 0) { return "zero"; } // We should add minus before conversion if the number is less than 0 ( for negative number) if (number < 0) { //convert the number to a string String numberStr = "" + number; // remove minus before the number numberStr = numberStr.substring(1); // add minus before the number and convert the rest of number return "minus " + convertNumberToWords(Integer.parseInt(numberStr)); } // Here We check if number is divisible by 1 million if ((number / 1000000) > 0) { words += convertNumberToWords(number / 1000000) + " million "; number %= 1000000; } // Here we are checking if number is divisible by 1 thousand if ((number / 1000) > 0) { words += convertNumberToWords(number / 1000) + " thousand "; number %= 1000; } // Here we are checking if number is divisible by 1 hundred if ((number / 100) > 0) { words += convertNumberToWords(number / 100) + " hundred "; number %= 100; } if (number > 0) { // check if number is within unitsArray if (number < 20) { //fetch the appropriate value from unit array words += unitsArray[number]; } else { // fetch the appropriate value from tens array words += tensArray[number / 10]; if ((number % 10) > 0) { words += "-" + unitsArray[number % 10]; } } } return words; } } |
I have checked above program for few int datatype inputs but you can check it for any input within Integer.MAX_VALUE-1 range. if you want, program should work for larger than Integer MAX range then you have to modified this program to take input in either long or BigInteger.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="font-size: 12pt;">Please Enter a number(max upto 9 digits): 0 Number in words: zero Please Enter a number(max upto 9 digits): 256 Number in words: two hundred fifty-six Please Enter a number(max upto 9 digits): 80952 Number in words: eighty thousand nine hundred fifty-two Please Enter a number(max upto 9 digits): 80952 Number in words: eighty thousand nine hundred fifty-two</span> |
That’s all about How to convert number to words in java
You May Also Like:
How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Java program to Remove Duplicate Elements From an Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
Java Program to find duplicate elements in an Array
Count number of words in a string in java
How would you check if a number is even or odd using bit wise operator in Java?
How can you check if the given number is power of 2?
How to check if String is number in java ?
If you have any feedback or suggestion please feel free to drop in blow comment box.