I want to write a program in java such a way that if i enter a number the number should be displayed in words ex: 64=SixtyFour
Amount in words
Collapse
X
-
Tags: None
-
Originally posted by roshancaptain1I want to write a program in java such a way that if i enter a number the number should be displayed in words ex: 64=SixtyFourComment
-
There are two approaches (depending on whether you have a maximum number that can be entered). If you do an array of Strings of worded-numbers would quickly reference any number.
Otherwise you will have to parse each number and decide how to display it. This is easy enough for the x*10^2 (x hundred) digit (like wise thousand or anything greater) but the most difficult would be the y*10^1 (y "ty" - because construct not always regular (fifty, thirty, twenty) so you have to think of special cases.
Hopefully this will give you a start.....Comment
-
Try this
Code:package com; public class NumberToString { public final static String[] arr={"","one","two","three","four","five","six","seven","eight","nine"}; public final static String[] arr2={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; public static void main (String args[]) { int num = 991762348; String number=""; //System.out.println(num/100000000); if(num/1000000>0) { number = number+converter(num/1000000)+ "million "; num=num%1000000; } if(num/1000>0) { number = number+converter(num/1000)+ "thousand "; num=num%1000; } if(num>0) { number = number+converter(num); } System.out.println(number); } public static String converter(int num) { String str=""; int num2=num; if(num2/100>0) { str = str+arr[num2/100]+" hundred "; num2 =num2%100; } if(num2/10>0) { str = str+arr2[num2/10]+" "; num2 =num2%10; } if(num2>0) { str = str+arr[num2]+" "; } return str; } }
Comment
-
Originally posted by maverick19Try this
Code:package com; public class NumberToString { public final static String[] arr={"","one","two","three","four","five","six","seven","eight","nine"}; public final static String[] arr2={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; public static void main (String args[]) { int num = 991762348; String number=""; //System.out.println(num/100000000); if(num/1000000>0) { number = number+converter(num/1000000)+ "million "; num=num%1000000; } if(num/1000>0) { number = number+converter(num/1000)+ "thousand "; num=num%1000; } if(num>0) { number = number+converter(num); } System.out.println(number); } public static String converter(int num) { String str=""; int num2=num; if(num2/100>0) { str = str+arr[num2/100]+" hundred "; num2 =num2%100; } if(num2/10>0) { str = str+arr2[num2/10]+" "; num2 =num2%10; } if(num2>0) { str = str+arr[num2]+" "; } return str; } }
Comment
-
-
Originally posted by maverick19sorry, but i thought the purpose of these blogs was to solve the problemsComment
-
Thanku sir..i could get the solution for my problem..i felt its too long...but i am satisfied with my program ....i tried the program on my own....Comment
-
//****accept a value "amount2"** **//
if(amount2.leng th()==1)
{
a123="1111"+amo unt2;
}
if(amount2.leng th()==2)
{
a123="111"+amou nt2;
}
if(amount2.leng th()==3)
{
a123="11"+amoun t2;
}
if(amount2.leng th()==4)
{
a123="1"+amount 2;
}
if(amount2.leng th()>4)
{
a123=amount2;
}
n11a=a123.charA t(0);
n1a=n11a%10+2;
n22=a123.charAt (1);
n2=n22%10+2;
n33=a123.charAt (2);
n3=n33%10+2;
n44=a123.charAt (3);
n4=n44%10+2;
n15=a123.charAt (4);
n5=n15%10+2;
if(amount2.leng th()>4 && n1a==11 && n2==11)
{
n10="eleventhou sand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==2)
{
n10="twelvethou sand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==3)
{
n10="thirteenth ousand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==4)
{
n10="fourteenth ousand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==5)
{
n10="fifteentho usand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==6)
{
n10="sixteentho usand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==7)
{
n10="seventeent housand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==8)
{
n10="eighteenth ousand";
}
else if(amount2.leng th()>4 && n1a==11 && n2==9)
{
n10="nineteenth ousand";
}
else if(amount2.leng th()>4 && n1a==11)
{
n10="ten";
}
else if(amount2.leng th()>4 && n1a==2)
{
n10="twenty";
}
else if(amount2.leng th()>4 && n1a==3)
{
n10="thirty";
}
else if(amount2.leng th()>4 && n1a==4)
{
n10="forty";
}
else if(amount2.leng th()>4 && n1a==5)
{
n10="fifty";
}
else if(amount2.leng th()>4 && n1a==6)
{
n10="sixty";
}
else if(amount2.leng th()>4 && n1a==7)
{
n10="seventy";
}
else if(amount2.leng th()>4 && n1a==8)
{
n10="eighty";
}
else if(amount2.leng th()>4 && n1a==9)
{
n10="ninty";
}
else
{
n10="";
}
if(n1a==11 && amount2.length( )>4 && (n2==11||n2==2| |n2==3||n2==4|| n2==5||n2==6||n 2==7||n2==8||n2 ==9))
{
n12a="";
}
else if(amount2.leng th()>3 && n2==11)
{
n12a="one thousand";
}
else if(amount2.leng th()>3 && n2==2)
{
n12a="two thousand";
}
else if(amount2.leng th()>3 && n2==3)
{
n12a="three thousand";
}
else if(amount2.leng th()>3 && n2==4)
{
n12a="four thousand";
}
else if(amount2.leng th()>3 && n2==5)
{
n12a="five thousand";
}
else if(amount2.leng th()>3 && n2==6)
{
n12a="six thousand";
}
else if(amount2.leng th()>3 && n2==7)
{
n12a="seven thousand";
}
else if(amount2.leng th()>3 && n2==8)
{
n12a="eight thousand";
}
else if(amount2.leng th()>3 && n2==9)
{
n12a="nine thousand";
}
else if(amount2.leng th()==4)
{
n12a="thousand" ;
}
if(amount2.leng th()>2 && n3==11)
{
n13="one hundred";
}
else if(amount2.leng th()>2 && n3==2)
{
n13="two hundred";
}
else if(amount2.leng th()>2 && n3==3)
{
n13="three hundred";
}
else if(amount2.leng th()>2 && n3==4)
{
n13="four hundred";
}
else if(amount2.leng th()>2 && n3==5)
{
n13="five hundred";
}
else if(amount2.leng th()>2 && n3==6)
{
n13="six hundred";
}
else if(amount2.leng th()>2 && n3==7)
{
n13="seven hundred";
}
else if(amount2.leng th()>2 && n3==8)
{
n13="eight hundred";
}
else if(amount2.leng th()>2 && n3==9)
{
n13="nine hundred";
}
else
{
n13="";
}
if(amount2.leng th()>1 && n4==11 && n5==11)
{
n14="eleven";
}
else if(amount2.leng th()>1 && n4==11 && n5==2)
{
n14="twelve";
}
else if(amount2.leng th()>1 && n4==11 && n5==3)
{
n14="thirteen";
}
else if(amount2.leng th()>1 && n4==11 && n5==4)
{
n14="fourteen";
}
else if(amount2.leng th()>1 && n4==11 && n5==5)
{
n14="fifteen";
}
else if(amount2.leng th()>1 && n4==11 && n5==6)
{
n14="sixteen";
}
else if(amount2.leng th()>1 && n4==11 && n5==7)
{
n14="seventeen" ;
}
else if(amount2.leng th()>1 && n4==11 && n5==8)
{
n14="eighteen";
}
else if(amount2.leng th()>1 && n4==11 && n5==9)
{
n14="nineteen";
}
else if(amount2.leng th()>1 && n4==11)
{
n14="one";
}
else if( amount2.length( )>1 && n4==2)
{
n14="twenty";
}
else if( amount2.length( )>1 && n4==3)
{
n14="thirty";
}
else if(amount2.leng th()>1 && n4==4)
{
n14="forty";
}
else if(amount2.leng th()>1 && n4==5)
{
n14="fifty";
}
else if(amount2.leng th()>1 && n4==6)
{
n14="sixty";
}
else if(amount2.leng th()>1 && n4==7)
{
n14="seventy";
}
else if(amount2.leng th()>1 && n4==8)
{
n14="eighty";
}
else if(amount2.leng th()>1 && n4==9)
{
n14="ninty";
}
else
{
n14="";
}
if(n4==11 && amount2.length( )>1 && (n5==11||n5==11 ||n5==2||n5==3| |n5==4||n5==5|| n5==6||n5==7||n 5==8||n5==9))
{
n17="";
}
else if(n5==11)
{
n17="one ";
}
else if( n5==2)
{
n17="two ";
}
else if( n5==3)
{
n17="three ";
}
else if( n5==4)
{
n17="four ";
}
else if(n5==5)
{
n17="five ";
}
else if( n5==6)
{
n17="six ";
}
else if( n5==7)
{
n17="seven ";
}
else if( n5==8)
{
n17="eight ";
}
else if(n5==9)
{
n17="nine ";
}
else
{
n17="";
}
//***print the following**//
/*out.println(n1 0);
out.println(n12 a);
out.println(n13 );
out.println(n14 );
out.println(n17 );*/Comment
-
-
Originally posted by roshancaptain1sir.can u please send your codes....
The following is a version(more or less) that was posted here some time ago. I only made minor modifications to this one. It still needs modifications but only after you've understood it first.
Code:import javax.swing.JOptionPane; public class NumToWord { public static void main(String[] args){ String units[]= {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; String teens[]= {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; String tens[]= {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; String amntchar = ""; int amnt,q,n; amntchar = JOptionPane.showInputDialog("Enter Amount "); amnt = Integer.parseInt(amntchar); if((amnt/1000000000) !=0) { q= amnt/100000000; amnt = amnt-q*100000000; System.out.print(units[q] + " hundred "); if((amnt/10000000)==0) { if( (amnt/10000000)== 0) { System.out.print("billion "); } } } if(amnt>=2000000000 && amnt<=999999999) { if((amnt/10000000) !=0) { q= amnt/10000000; amnt = amnt-q*10000000; System.out.print(tens[q]); if((amnt/1000000) ==0) { System.out.print("billion "); } } } if((amnt/100000000) !=0) { q= amnt/100000000; amnt = amnt-q*100000000; System.out.print(units[q] + " hundred "); if((amnt/10000000)==0) { if( (amnt/10000000)== 0) { System.out.print("million "); } } } if(amnt>=20000000 && amnt<=99999999) { if((amnt/10000000) !=0) { q= amnt/10000000; amnt = amnt-q*10000000; System.out.print(tens[q]); if((amnt/1000000) ==0) { System.out.print("million "); } } } if((amnt/1000000) !=0) { q= amnt/1000000; amnt = amnt-q*1000000; System.out.print(units[q]+" million "); } if((amnt/100000) !=0) { q= amnt/100000; amnt = amnt-q*100000; System.out.print(units[q]+" hundred "); if((amnt/10000)==0) { if( (amnt/1000)== 0) { System.out.print("thousand "); } } } if(amnt>=20000 && amnt<=99999) { if((amnt/10000) !=0) { q= amnt/10000; amnt = amnt-q*10000; System.out.print(tens[q]); if((amnt/1000) ==0) { System.out.print("thousand "); } } } if((amnt/10000) !=0) { q= amnt/10000; amnt = amnt-q*10000; System.out.print(teens[q] +" thousand "); } if((amnt/1000) !=0) { q= amnt/1000; amnt = amnt-q*1000; System.out.print(units[q] +" thousand "); } if((amnt/100) !=0) { q= amnt/100; amnt = amnt-q*100; System.out.print(units[q]+ " hundred "); } for(n=1;n<=9;n++) { if(amnt== n) System.out.print(units[n]); for(n=10;n<=19;n++) if(amnt== n) System.out.print(teens[n-10]); if(amnt>= 20 && amnt<= 29) System.out.print(tens[2]+units[amnt-20]); if(amnt>= 30 && amnt<=39) System.out.print(tens[3]+units[amnt-30]); if(amnt>= 40 && amnt<= 49) System.out.print(tens[4]+units[amnt-40]); if(amnt>= 50 && amnt<=59) System.out.print(tens[5]+units[amnt-50]); if(amnt>= 60 && amnt<= 69) System.out.print(tens[6]+units[amnt-60]); if(amnt>= 70 && amnt<= 79) System.out.print(tens[7]+units[amnt-70]); if(amnt>= 80 && amnt<= 89) System.out.print(tens[8]+units[amnt-80]); if(amnt>= 90 && amnt<= 99) System.out.print(tens[9]+units[amnt-90]); } }
And please do not call me sir.Comment
-
i am very sorry to call u sir again...but its a respect for knowledge.I always look into ur solutions...are u in online now.
sir..actually i have done a project and iwant to include these codes in in. it is a servlet program and its a billing project. The amount which will be automatically caluculated will also be automatically displayed in words. Do u really want to see it i will have to send more than 1 filesComment
Comment