Amount in words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roshancaptain1
    New Member
    • Oct 2006
    • 14

    Amount in words

    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
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    OK. Have you tried writing it yourself?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by roshancaptain1
      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.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        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

        • maverick19
          New Member
          • Nov 2006
          • 25

          #5
          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

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by maverick19
            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;		
            	}
            }
            You could have let the OP try it first.

            Comment

            • maverick19
              New Member
              • Nov 2006
              • 25

              #7
              sorry, but i thought the purpose of these blogs was to solve the problems

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by maverick19
                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.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  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.

                  Comment

                  • roshancaptain1
                    New Member
                    • Oct 2006
                    • 14

                    #10
                    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

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by roshancaptain1
                      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?

                      Comment

                      • roshancaptain1
                        New Member
                        • Oct 2006
                        • 14

                        #12
                        //****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

                        • roshancaptain1
                          New Member
                          • Oct 2006
                          • 14

                          #13
                          sir.can u please send your codes....

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by roshancaptain1
                            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.


                            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

                            • roshancaptain1
                              New Member
                              • Oct 2006
                              • 14

                              #15
                              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

                              Comment

                              Working...