438,739 Members | 2,014 Online Need help? Post your question and get tips & solutions from a community of 438,739 IT Pros & Developers. It's quick & easy.

# Amount in words

 P: 14 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 Dec 12 '06 #1
16 Replies

 Expert 2.5K+ P: 3,652 OK. Have you tried writing it yourself? Dec 12 '06 #2

 10K+ P: 13,264 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 Good. I already have a program for doing it but I'm sure you can come up with a much better one. So have a go at it and let's see what you can come up with. Dec 12 '06 #3

 100+ P: 1,806 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..... Dec 12 '06 #4

 P: 25 Try this Expand|Select|Wrap|Line Numbers 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;             } }     Dec 13 '06 #5

 10K+ P: 13,264 Try this Expand|Select|Wrap|Line Numbers 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;             } }     You could have let the OP try it first. Dec 13 '06 #6

 P: 25 sorry, but i thought the purpose of these blogs was to solve the problems Dec 13 '06 #7

 10K+ P: 13,264 sorry, but i thought the purpose of these blogs was to solve the problems Yes it is. We just don't want to cripple students in the process by dumping homework solutions on them. It would be better if they learned something from their questions by getting help on doing their homework rather than just getting someone to do their homework for them. Dec 13 '06 #8

 Expert 2.5K+ P: 3,652 Plus, if the OP gets the entire solution without doing any work, this qualifies as cheating, which I'm sure is frowned upon in Universities. Dec 14 '06 #9

 P: 14 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.... Dec 18 '06 #10

 10K+ P: 13,264 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.... Could you post what you've done then? Dec 18 '06 #11

 P: 14 //****accept a value "amount2"****// if(amount2.length()==1) { a123="1111"+amount2; } if(amount2.length()==2) { a123="111"+amount2; } if(amount2.length()==3) { a123="11"+amount2; } if(amount2.length()==4) { a123="1"+amount2; } if(amount2.length()>4) { a123=amount2; } n11a=a123.charAt(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.length()>4 && n1a==11 && n2==11) { n10="eleventhousand"; } else if(amount2.length()>4 && n1a==11 && n2==2) { n10="twelvethousand"; } else if(amount2.length()>4 && n1a==11 && n2==3) { n10="thirteenthousand"; } else if(amount2.length()>4 && n1a==11 && n2==4) { n10="fourteenthousand"; } else if(amount2.length()>4 && n1a==11 && n2==5) { n10="fifteenthousand"; } else if(amount2.length()>4 && n1a==11 && n2==6) { n10="sixteenthousand"; } else if(amount2.length()>4 && n1a==11 && n2==7) { n10="seventeenthousand"; } else if(amount2.length()>4 && n1a==11 && n2==8) { n10="eighteenthousand"; } else if(amount2.length()>4 && n1a==11 && n2==9) { n10="nineteenthousand"; } else if(amount2.length()>4 && n1a==11) { n10="ten"; } else if(amount2.length()>4 && n1a==2) { n10="twenty"; } else if(amount2.length()>4 && n1a==3) { n10="thirty"; } else if(amount2.length()>4 && n1a==4) { n10="forty"; } else if(amount2.length()>4 && n1a==5) { n10="fifty"; } else if(amount2.length()>4 && n1a==6) { n10="sixty"; } else if(amount2.length()>4 && n1a==7) { n10="seventy"; } else if(amount2.length()>4 && n1a==8) { n10="eighty"; } else if(amount2.length()>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||n2==7| |n2==8||n2==9)) { n12a=""; } else if(amount2.length()>3 && n2==11) { n12a="one thousand"; } else if(amount2.length()>3 && n2==2) { n12a="two thousand"; } else if(amount2.length()>3 && n2==3) { n12a="three thousand"; } else if(amount2.length()>3 && n2==4) { n12a="four thousand"; } else if(amount2.length()>3 && n2==5) { n12a="five thousand"; } else if(amount2.length()>3 && n2==6) { n12a="six thousand"; } else if(amount2.length()>3 && n2==7) { n12a="seven thousand"; } else if(amount2.length()>3 && n2==8) { n12a="eight thousand"; } else if(amount2.length()>3 && n2==9) { n12a="nine thousand"; } else if(amount2.length()==4) { n12a="thousand"; } if(amount2.length()>2 && n3==11) { n13="one hundred"; } else if(amount2.length()>2 && n3==2) { n13="two hundred"; } else if(amount2.length()>2 && n3==3) { n13="three hundred"; } else if(amount2.length()>2 && n3==4) { n13="four hundred"; } else if(amount2.length()>2 && n3==5) { n13="five hundred"; } else if(amount2.length()>2 && n3==6) { n13="six hundred"; } else if(amount2.length()>2 && n3==7) { n13="seven hundred"; } else if(amount2.length()>2 && n3==8) { n13="eight hundred"; } else if(amount2.length()>2 && n3==9) { n13="nine hundred"; } else { n13=""; } if(amount2.length()>1 && n4==11 && n5==11) { n14="eleven"; } else if(amount2.length()>1 && n4==11 && n5==2) { n14="twelve"; } else if(amount2.length()>1 && n4==11 && n5==3) { n14="thirteen"; } else if(amount2.length()>1 && n4==11 && n5==4) { n14="fourteen"; } else if(amount2.length()>1 && n4==11 && n5==5) { n14="fifteen"; } else if(amount2.length()>1 && n4==11 && n5==6) { n14="sixteen"; } else if(amount2.length()>1 && n4==11 && n5==7) { n14="seventeen"; } else if(amount2.length()>1 && n4==11 && n5==8) { n14="eighteen"; } else if(amount2.length()>1 && n4==11 && n5==9) { n14="nineteen"; } else if(amount2.length()>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.length()>1 && n4==4) { n14="forty"; } else if(amount2.length()>1 && n4==5) { n14="fifty"; } else if(amount2.length()>1 && n4==6) { n14="sixty"; } else if(amount2.length()>1 && n4==7) { n14="seventy"; } else if(amount2.length()>1 && n4==8) { n14="eighty"; } else if(amount2.length()>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||n5==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(n10); out.println(n12a); out.println(n13); out.println(n14); out.println(n17);*/ Dec 18 '06 #12

 P: 14 sir.can u please send your codes.... Dec 18 '06 #13

 10K+ P: 13,264 sir.can u please send your codes.... Your codes are very long. Post the complete program using code tags. I'd like to see if they really work. 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. Expand|Select|Wrap|Line Numbers  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+units[amnt-20]); if(amnt>= 30 && amnt<=39) System.out.print(tens+units[amnt-30]); if(amnt>= 40 && amnt<= 49) System.out.print(tens+units[amnt-40]); if(amnt>= 50 && amnt<=59) System.out.print(tens+units[amnt-50]); if(amnt>= 60 && amnt<= 69) System.out.print(tens+units[amnt-60]); if(amnt>= 70 && amnt<= 79) System.out.print(tens+units[amnt-70]); if(amnt>= 80 && amnt<= 89) System.out.print(tens+units[amnt-80]); if(amnt>= 90 && amnt<= 99) System.out.print(tens+units[amnt-90]);   } }     And please do not call me sir. Dec 18 '06 #14

 P: 14 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 files Dec 18 '06 #15

 10K+ P: 13,264 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 files You can send the code for the servlet Dec 18 '06 #16

 10K+ P: 13,264 Are you still there? Dec 21 '06 #17 