By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,739 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
16 Replies


Ganon11
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

DeMan
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
  1. package com;
  2.  
  3. public class NumberToString {
  4.  
  5.     public final static String[] arr={"","one","two","three","four","five","six","seven","eight","nine"};
  6.     public final static String[] arr2={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
  7.     public static void main (String args[])
  8.     {
  9.         int num = 991762348;
  10.         String number="";
  11.         //System.out.println(num/100000000);
  12.         if(num/1000000>0)
  13.         {
  14.             number = number+converter(num/1000000)+ "million ";
  15.             num=num%1000000;
  16.         }
  17.         if(num/1000>0)
  18.         {
  19.             number = number+converter(num/1000)+ "thousand ";
  20.             num=num%1000;
  21.         }
  22.         if(num>0)
  23.         {
  24.             number = number+converter(num);
  25.         }
  26.         System.out.println(number);
  27.     }
  28.     public static String converter(int num)
  29.     {
  30.         String str="";
  31.         int num2=num;
  32.         if(num2/100>0)
  33.         {
  34.             str = str+arr[num2/100]+" hundred ";
  35.             num2 =num2%100;
  36.         }
  37.         if(num2/10>0)
  38.         {
  39.             str = str+arr2[num2/10]+" ";
  40.             num2 =num2%10;
  41.         }
  42.         if(num2>0)
  43.         {
  44.             str = str+arr[num2]+" ";
  45.         }
  46.  
  47.         return str;        
  48.     }
  49. }
  50.  
  51.  
Dec 13 '06 #5

10K+
P: 13,264
Try this
Expand|Select|Wrap|Line Numbers
  1. package com;
  2.  
  3. public class NumberToString {
  4.  
  5.     public final static String[] arr={"","one","two","three","four","five","six","seven","eight","nine"};
  6.     public final static String[] arr2={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
  7.     public static void main (String args[])
  8.     {
  9.         int num = 991762348;
  10.         String number="";
  11.         //System.out.println(num/100000000);
  12.         if(num/1000000>0)
  13.         {
  14.             number = number+converter(num/1000000)+ "million ";
  15.             num=num%1000000;
  16.         }
  17.         if(num/1000>0)
  18.         {
  19.             number = number+converter(num/1000)+ "thousand ";
  20.             num=num%1000;
  21.         }
  22.         if(num>0)
  23.         {
  24.             number = number+converter(num);
  25.         }
  26.         System.out.println(number);
  27.     }
  28.     public static String converter(int num)
  29.     {
  30.         String str="";
  31.         int num2=num;
  32.         if(num2/100>0)
  33.         {
  34.             str = str+arr[num2/100]+" hundred ";
  35.             num2 =num2%100;
  36.         }
  37.         if(num2/10>0)
  38.         {
  39.             str = str+arr2[num2/10]+" ";
  40.             num2 =num2%10;
  41.         }
  42.         if(num2>0)
  43.         {
  44.             str = str+arr[num2]+" ";
  45.         }
  46.  
  47.         return str;        
  48.     }
  49. }
  50.  
  51.  
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

Ganon11
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
  1.  import javax.swing.JOptionPane; 
  2. public class NumToWord {
  3.  public static void main(String[] args){
  4.   String units[]= {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
  5.   String teens[]= {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
  6.   String tens[]= {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
  7.   String amntchar = "";
  8.   int amnt,q,n;
  9.   amntchar = JOptionPane.showInputDialog("Enter Amount ");
  10.   amnt = Integer.parseInt(amntchar);
  11.   if((amnt/1000000000) !=0) {
  12.    q= amnt/100000000;
  13.    amnt = amnt-q*100000000;
  14.    System.out.print(units[q] + " hundred ");
  15.    if((amnt/10000000)==0) {
  16.     if( (amnt/10000000)== 0) {
  17.      System.out.print("billion ");
  18.     }
  19.    }
  20.   }
  21.   if(amnt>=2000000000 && amnt<=999999999) {
  22.    if((amnt/10000000) !=0) {
  23.     q= amnt/10000000;
  24.     amnt = amnt-q*10000000;
  25.     System.out.print(tens[q]);
  26.     if((amnt/1000000) ==0) {
  27.      System.out.print("billion ");
  28.     }
  29.    }
  30.   }
  31.   if((amnt/100000000) !=0) {
  32.    q= amnt/100000000;
  33.    amnt = amnt-q*100000000;
  34.    System.out.print(units[q] + " hundred ");
  35.    if((amnt/10000000)==0) {
  36.     if( (amnt/10000000)== 0) {
  37.      System.out.print("million ");
  38.     }
  39.    }
  40.   }
  41.   if(amnt>=20000000 && amnt<=99999999) {
  42.    if((amnt/10000000) !=0) {
  43.     q= amnt/10000000;
  44.     amnt = amnt-q*10000000;
  45.     System.out.print(tens[q]);
  46.     if((amnt/1000000) ==0) {
  47.      System.out.print("million ");
  48.     }
  49.    }
  50.   }
  51.   if((amnt/1000000) !=0) {
  52.    q= amnt/1000000;
  53.    amnt = amnt-q*1000000;
  54.    System.out.print(units[q]+" million ");
  55.   }
  56.   if((amnt/100000) !=0) {
  57.    q= amnt/100000;
  58.    amnt = amnt-q*100000;
  59.    System.out.print(units[q]+" hundred ");
  60.    if((amnt/10000)==0) {
  61.     if( (amnt/1000)== 0) {
  62.      System.out.print("thousand ");
  63.     }
  64.    }
  65.   }
  66.   if(amnt>=20000 && amnt<=99999) {
  67.    if((amnt/10000) !=0) {
  68.     q= amnt/10000;
  69.     amnt = amnt-q*10000;
  70.     System.out.print(tens[q]);
  71.     if((amnt/1000) ==0) {
  72.      System.out.print("thousand ");
  73.     }
  74.    }
  75.   }
  76.   if((amnt/10000) !=0) {
  77.    q= amnt/10000;
  78.    amnt = amnt-q*10000;
  79.    System.out.print(teens[q] +" thousand ");
  80. }
  81. if((amnt/1000) !=0)
  82. {
  83. q= amnt/1000;
  84. amnt = amnt-q*1000;
  85. System.out.print(units[q] +" thousand ");
  86. }
  87. if((amnt/100) !=0)
  88. {
  89. q= amnt/100;
  90. amnt = amnt-q*100;
  91. System.out.print(units[q]+ " hundred ");
  92. }
  93. for(n=1;n<=9;n++) {
  94. if(amnt== n)
  95. System.out.print(units[n]);
  96. for(n=10;n<=19;n++)
  97. if(amnt== n)
  98. System.out.print(teens[n-10]);
  99. if(amnt>= 20 && amnt<= 29)
  100. System.out.print(tens[2]+units[amnt-20]);
  101. if(amnt>= 30 && amnt<=39)
  102. System.out.print(tens[3]+units[amnt-30]);
  103. if(amnt>= 40 && amnt<= 49)
  104. System.out.print(tens[4]+units[amnt-40]);
  105. if(amnt>= 50 && amnt<=59)
  106. System.out.print(tens[5]+units[amnt-50]);
  107. if(amnt>= 60 && amnt<= 69)
  108. System.out.print(tens[6]+units[amnt-60]);
  109. if(amnt>= 70 && amnt<= 79)
  110. System.out.print(tens[7]+units[amnt-70]);
  111. if(amnt>= 80 && amnt<= 89)
  112. System.out.print(tens[8]+units[amnt-80]);
  113. if(amnt>= 90 && amnt<= 99)
  114. System.out.print(tens[9]+units[amnt-90]);
  115.  
  116. }
  117. }
  118.  
  119.  


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

Post your reply

Sign in to post your reply or Sign up for a free account.