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
Really its very nice program.i hav go through many websites but i cant find s efficient one like yours. Thank you sir..
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?
“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;
}
?>
Dear sir,
the contents are really nice.
please maintain the order of contents
Thank you
very nice programming .
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)
Derive logic yourself. This is an example code. Your requirements should be customized by yourself only.
Thank you for the post. Its very useful
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
A program is available in way2java:
http://way2java.com/arrays/converting-numbers-to-words/
but sir i need it for decimal also… for 12.22 it should print it as twelve point two two
I gave good example. Customise to your needs. But do not ask everything from the other people.
its not at all good.. thank u
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.
sir, i want decimal convertion too.. can u plz help me
Be elaborate. What you mean by decimal conversion. You mean decimal, binary, octet and hexa.
i understand that it is done using arrays and string..i would like to know the logic of the program…what is the function of pw??
pw is method name, for which parameters are passed.
am beginner in java..can u please explain this code to me??
It is with arrays and strings.
this prog really help me ……… thanks:)
Both chat client and chat server are given.
can anybody explain the code…………please
It simple arrays program.
Thanks…
Sir can u plz explain me the program i didnt under stand some steps??
I used arrays and strings. If you are aware of C-lang arrays, it is enough to understand.
ok sir if i given n=25 then System.out.print(ten[25/10]+” “+one[25%10]); from this statement how the value is extracted?
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.
What do you say? Any problem in the program. Let me know.
Sir,
I am awaiting your reply for this bug in this program
Hello,
The bug is fixed. Now you can refer the program in the site.
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.
Yes, I too checked. I will fix the bug and let you know shortly.
it was very helpful. but i need this program in swing package…. can u please send it in swing package
Do you know Swing? Start the work and whenever you get stuck up, I help. But do not ask anyone to write complete code for you. Then when you will you develop?