# Converting Numbers to Words

Sometimes it is very much required in coding to convert numbers to words and print them as can be seen in Railway reservation tickets. Explained in "Converting Numbers to Words" with Java syntax.

Following program illustrates "Converting Numbers to Words".

```import java.util.*;
public class YourNumberMyWord
{
public void pw(int n,String ch)
{
String  one[]={" "," one"," two"," three"," four"," five"," six"," seven"," eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};

String ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};

if(n > 19) { System.out.print(ten[n/10]+" "+one[n%10]);} else { System.out.print(one[n]);}
if(n > 0)System.out.print(ch);
}
public static void main(String[] args)
{
int n=0;
Scanner scanf = new Scanner(System.in);
System.out.println("Enter an integer number: ");
n = scanf.nextInt();

if(n < = 0)
System.out.println("Enter numbers greater than 0");
}
else
{
YourNumberMyWord a = new YourNumberMyWord();
a.pw((n/1000000000)," Hundred");
a.pw((n/10000000)%100," crore");
a.pw(((n/100000)%100)," lakh");
a.pw(((n/1000)%100)," thousand");
a.pw(((n/100)%10)," hundred");
a.pw((n%100)," ");
}
}
}
``` The integer number is taken from keyboard, converted and printed in words. The number can be taken from a file or database or GUI also. Here, java.util.Scanner class is used to take keyboard input. Other styles of keyboard input are using the classes DataInputStream and BufferedReader.

Code is self-explanatory, involves small logic of separating each digit in the integer number, converting each digit to an array of words, exchanging the word and printing each word.

Precaution: Write as if(n<=0) All ARRAY Operations at a Glance

Constructor related Topics

### 36 thoughts on “Converting Numbers to Words”

1. Really its very nice program.i hav go through many websites but i cant find s efficient one like yours. Thank you sir..

2. Hi Sir,

Could you please explain the logic of these codes below?

a.pw((n/1000000000),” Hundred”);
a.pw((n/10000000)%100,” crore”);
a.pw(((n/100000)%100),” lakh”);
a.pw(((n/1000)%100),” thousand”);
a.pw(((n/100)%10),” hundred”);
a.pw((n%100),” “);

I see this values add suffix, but how does it picks up and how the logic here works?

3. “Crore”,
100000 =>”Lakh”,
1000 =>”Thousand”,
100 =>”Hundred”,

90 =>”Ninety”,
80 =>”Eighty”,
70 =>”Seventy”,
60 =>”Sixty”,
50 =>”Fifty”,
40 =>”Forty”,
30 =>”Thirty”,
20 =>”Twenty”,

19 =>”Nineteen”,
18 =>”Eighteen”,
17 =>”Seventeen”,
16 =>”Sixteen”,
15 =>”Fifteen”,
14 =>”Fourteen”,
13 =>”Thirteen”,
12 =>”Twelve”,
11 =>”Eleven”,

10 =>”Ten”,
9 =>”Nine”,
8 =>”Eight”,
7 =>”Seven”,
6 =>”Six”,
5 =>”Five”,
4 =>”Four”,
3 =>”Three”,
2 =>”Two”,
1 =>”One”];

\$n = \$num;

if(\$n\$val) {

if( \$base = 100)
\$str .= Num_Words(\$factor).” “.\$set[\$base];
else
\$str .= ” “.\$set[\$base];

}
}
return \$str;
}
?>

4. Sir How to print nos greater than crore i.e for input like below
Please enter the number to convert it to Roman.
5674389097
java.util.InputMismatchException: For input string: “5674389097”
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at javaBasics.NumberToRoman.convertNumberToRoman(NumberToRoman.java:25)
at javaBasics.NumberToRoman.main(NumberToRoman.java:12)

5. i need a conversion from decimal to word like 999999999.99 to ninety nine crores ninety nine lacks ninety nine thousands nine hundred and ninety nine point nine nine

1. but sir i need it for decimal also… for 12.22 it should print it as twelve point two two

1. Spoon feeding is SIN in programming.It’s really good and concise code.
You need to bit work hard to understand the goodness of code.
Thanks and take things sportively to improve further.

25/10=2;
25%10=5;

System.out.println(ten[25/10]+” “+one[25%10]);

Replace the above values… here ten, one are array names. Observe them.

6. Sir,
When I give the input numbers greater than 9999, the program “Converting Numbers to Words” in your site giving an exception.

Can I know the reason why it is so?

Thanks & regards
Jeevan.

1. it was very helpful. but i need this program in swing package…. can u please send it in swing package