How to convert a numeric value into English words in java
Source code
import java.util.Scanner;
/**
* Number to word convertor
*/
/**
* @author consfedkozhikode@gmail.com
*
*/
public class NumberToWord {
private String[] ones = {"","one","two","three","four","five","six","seven","eight","nine","ten"};
private String[] twenties = {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
private String[] tens = {"","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
private String[] hundreds = {"ones","tens","hundred","thousand","thousand","lakhs","lakhs","crore","crore"};
private String word = "";
private String tempWord = null;
private int lastDigit = 0;
/**
* @param args
*/
public static void main(String[] args) {
NumberToWord numberToWord = new NumberToWord();
int number = 0;
Scanner input = new Scanner(System.in);
System.out.println(" Enter the number : ");
number = input.nextInt();
int output = numberToWord.convertToWord(number,0);
}
private int convertToWord(int number,int count) {
int quotient = number/10;
int reminder = number % 10;
getTheWord(reminder,count);
if(quotient<=0){
word = tempWord + word;
System.out.println(" output : "+word);
return 0;
}else
{
return convertToWord(quotient,count+1);
}
}
private void getTheWord(int reminder,int placeValue) {
String newWord = null;
switch (placeValue) {
case 0:
newWord = " "+ones[reminder];
break;
case 1:
if(reminder==1){
word = word; //skip
int number = Integer.parseInt(String.valueOf(reminder+""+lastDigit));
newWord = " "+twenties[number];
}else{
word = tempWord + word;
newWord = " "+tens[reminder];
}
break;
case 2:
word = tempWord + word; // adding tens
newWord = " "+ones[reminder]+" "+hundreds[placeValue]+" and";
break;
case 3:
word = tempWord + word; //adding hundred
newWord = " "+ones[reminder]+" "+hundreds[placeValue];
break;
case 4:
if(reminder==1){
word = word;
int number = Integer.parseInt(String.valueOf(reminder+""+lastDigit));
newWord = ""+twenties[number]+" "+hundreds[placeValue]+"";
}else{
word = tempWord + word;
newWord = " "+tens[reminder]+"";
}
break;
default:
System.out.println(" This is default statement, it won't print ");
break;
}
lastDigit = reminder;
tempWord = newWord;
}
}
Output
| Number to english word in java |
Description
Number to word converter java program converts numeric value into words, currently the program will convert up to thousands. The same logic is applied for lakhs and crores so you can extend the project, only thing you have to do is to increase case statement in switch (repeating case 3 and case 4).
In this program I pass the user inputted number to recursive function convertToWord(), A recursive function is a function that call itself. convertToWord() function will find the quotient and reminder of the number in each calls, the reminder is send getTheWord() function where a switch case will convert the number into corresponding words.
Most useful code
Java reference ( Buy a copy )
Similar posts
Finding HCF and LCM
Finding Factorial using recursion
Fibonacci series
Binary search in java
Palindrome in java
Geometric mean using java
Prime number in java
Print triangle in java
Find missing number in java
author : +belazy
Ever tried to develop a tic toc toe game, you can try our code, multiplayer and with computer the whole source code will be available soon, stay update
http://javabelazy.blogspot.in/