File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Numeric value convert to word  format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Numeric value convert to word  format" Watch "Numeric value convert to word  format" New topic
Author

Numeric value convert to word format

Mahendra Darwatkar
Greenhorn

Joined: Apr 07, 2000
Posts: 20
Hi !!
Can anybody guide me how can i convert value 125 to word format "One Hundred Twenty Five"

Awaiting reply...
rgds,
Mahendra
Govinda
Greenhorn

Joined: Nov 20, 2000
Posts: 29
here is the way with which you can convert any number
fall in to int range of java primitive . here is a class named
parseInteger inside that a static method is available there
so you can use that static method in your requird place or
i also give a main method in this class which accept a
command line parameter and shows the word value according to it.
i convert the number in indiam number system. means
1432456 = fourteen lacs thirty two thousand fifty six.
and i used Arab and Kharab in place of million and billion and trillion .
i think better you run it and see how it convert number from digits to words.
in the case of any modification required either do yourself or write on javaranch in the same thread. i will do that for you. probabely when you see the logic behind this , you can easily made your required modifications.
for run this program simple compile by javac and then
run as java ParseInteger 123456
you see the output
One Lacs Twenty Three Thousand Four hundread fifty six.
one more thing i didn't use exception handling mechanism. so if you required do it to made your code more robust. and although the only mistakes that can happened will be at the time of giving input via command line or to static method, so if you take care in both of these concerns , i don't think any exception may occurd ever.
copy this code by cut & paste from the bottom version. i am writing code here in two places one below another. first is formatted that may cause some irritation at the time of
paste but the second one didn't . so if you copy , do it from bottom version.
<pre>
public class ParseInteger {
static String parseDigitToWords(int number1) {
String number = new Integer(number1).toString();
String token[] = new String[(number.length()%2 == 0)?number.length()/2+1 int)(number.length()/2.0)+1];
int i=0;
for (int j=0;number.length()>j ;)
{ String a;
a=String.valueOf(number.charAt(number.length()-(j+=1)));
if ((j==3)| |(number.length()==j))
{ token[i++]=a; continue;}
else
{token[i++]=number.charAt(number.length()-(j+=1))+a;
}
}
number = "";
for (int j=0;j<token.length;j++){<br /> String d2c="";<br /> switch(((token[j].length()==2))?token[j].charAt(0):98)<br /> { case '0' : break;<br /> case '1' : switch(token[j].charAt(1))<br /> { case '0': d2c+=" Ten";break;<br /> case '1': d2c+=" Eleven";break;<br /> case '2': d2c+=" Twelve";break;<br /> case '3': d2c+=" Thirteen";break;<br /> case '4': d2c+=" Fourteen";break;<br /> case '5': d2c+=" Fifteen";break;<br /> case '6': d2c+=" Sixteen";break;<br /> case '7': d2c+=" Seventeen";break;<br /> case '8': d2c+=" Eighteen";break;<br /> case '9': d2c+=" Nineteen";break;<br /> } break;<br /> case '2': d2c+=" Twenty";break;<br /> case '3': d2c+=" Thirty";break;<br /> case '4': d2c+=" Fourty";break;<br /> case '5': d2c+=" Fifty";break;<br /> case '6': d2c+=" Sixty";break;<br /> case '7': d2c+=" Seventy";break;<br /> case '8': d2c+=" Eighty";break;<br /> case '9': d2c+=" Ninety";break;<br /> } <br /> switch(((token[j].length()==2)&&(token[j].charAt(0)!='1'))?token[j].charAt(1):token[j].charAt(0)) <br /> {<br /> case '0' : break;<br /> case '1' : if (token[j].length()==1| |((token[j].length()==2)&&!(token[j].charAt(0)=='1'))) d2c += " One";break;<br /> case '2' : d2c += " Two";break;<br /> case '3' : d2c += " Three";break;<br /> case '4' : d2c += " Four";break;<br /> case '5' : d2c += " Five";break;<br /> case '6' : d2c += " Six";break;<br /> case '7' : d2c += " Seven";break;<br /> case '8' : d2c += " Eight";break;<br /> case '9' : d2c += " Nine";break;<br /> <br /> }<br /> switch(j)<br /> {<br /> case 0 : break;<br /> case 1 : if (d2c.trim().length()!=0) d2c+=" Hundread";break;<br /> case 2 : if (d2c.trim().length()!=0) d2c+=" Thousand";break;<br /> case 3 : if (d2c.trim().length()!=0) d2c+=" Lacs";break;<br /> case 4 : if (d2c.trim().length()!=0) d2c+=" Crore";break;<br /> case 5 : if (d2c.trim().length()!=0) d2c+=" Arab";break;<br /> case 6 : if (d2c.trim().length()!=0) d2c+=" Kharab";break;<br /> }<br /> number = d2c+number;<br /> }<br /> return number;<br /> }<br /> public static void main(String arg[]) {<br /> System.out.println(parseDigitToWords(new Integer(arg[0]).intValue()));<br /> } }<br /> <br /> </pre><br /> <br /> bottom version --> not formatted , use copy & paste from here will be free of rest of some irritation.
public class ParseInteger {
static String parseDigitToWords(int number1) {
String number = new Integer(number1).toString();
String token[] = new String[(number.length()%2 == 0)?number.length()/2+1 int)(number.length()/2.0)+1];
int i=0;
for (int j=0;number.length()>j ;)
{ String a;
a=String.valueOf(number.charAt(number.length()-(j+=1)));
if ((j==3)| |(number.length()==j))
{ token[i++]=a; continue;}
else
{token[i++]=number.charAt(number.length()-(j+=1))+a;
}
}
number = "";
for (int j=0;j<token.length;j++){>
String d2c="";
switch(((token[j].length()==2))?token[j].charAt(0):98)
{ case '0' : break;
case '1' : switch(token[j].charAt(1))
{ case '0': d2c+=" Ten";break;
case '1': d2c+=" Eleven";break;
case '2': d2c+=" Twelve";break;
case '3': d2c+=" Thirteen";break;
case '4': d2c+=" Fourteen";break;
case '5': d2c+=" Fifteen";break;
case '6': d2c+=" Sixteen";break;
case '7': d2c+=" Seventeen";break;
case '8': d2c+=" Eighteen";break;
case '9': d2c+=" Nineteen";break;
} break;
case '2': d2c+=" Twenty";break;
case '3': d2c+=" Thirty";break;
case '4': d2c+=" Fourty";break;
case '5': d2c+=" Fifty";break;
case '6': d2c+=" Sixty";break;
case '7': d2c+=" Seventy";break;
case '8': d2c+=" Eighty";break;
case '9': d2c+=" Ninety";break;
}
switch(((token[j].length()==2)&&(token[j].charAt(0)!='1'))?token[j].charAt(1):token[j].charAt(0))
{
case '0' : break;
case '1' : if (token[j].length()==1| |((token[j].length()==2)&&!(token[j].charAt(0)=='1'))) d2c += " One";break;
case '2' : d2c += " Two";break;
case '3' : d2c += " Three";break;
case '4' : d2c += " Four";break;
case '5' : d2c += " Five";break;
case '6' : d2c += " Six";break;
case '7' : d2c += " Seven";break;
case '8' : d2c += " Eight";break;
case '9' : d2c += " Nine";break;

}
switch(j)
{
case 0 : break;
case 1 : if (d2c.trim().length()!=0) d2c+=" Hundread";break;
case 2 : if (d2c.trim().length()!=0) d2c+=" Thousand";break;
case 3 : if (d2c.trim().length()!=0) d2c+=" Lacs";break;
case 4 : if (d2c.trim().length()!=0) d2c+=" Crore";break;
case 5 : if (d2c.trim().length()!=0) d2c+=" Arab";break;
case 6 : if (d2c.trim().length()!=0) d2c+=" Kharab";break;
}
number = d2c+number;
}
return number;
}
public static void main(String arg[]) {
System.out.println(parseDigitToWords(new Integer(arg[0]).intValue()));
} }

[This message has been edited by Govinda (edited November 28, 2000).]
Mahendra Darwatkar
Greenhorn

Joined: Apr 07, 2000
Posts: 20
Thank you very much Govinda,,
Your program is working fine. I got exact output which I want..
I tried for amount calculation.. but for paise like 50 paisa or 25 paisa is you got any other program for this..
I am very very much thankful to you...
Mahendra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Numeric value convert to word format
 
Similar Threads
Numeric value convert to word format
Numeric value convert to word format
Numeric value convert to word format
Acrobat reader question
Logos eps format