Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-30-2008, 01:30 AM
Member
 
Join Date: Aug 2008
Posts: 2
Rep Power: 0
fertas is on a distinguished road
Default number to word with decimals
hi

im having a problem on how to convert number with decimal point. For example when the user enter 123456 it should print like this "1,234.56" and reads as "one thousand two hundred thirty four and fifty six".

here's the code:

import java.util.regex.*;
import java.io.*;


public class CStudy2
{public static String ones[]={"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ",
"Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
public static String tens[]={ "Twenty ","Thirty ","Fourty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
public static String prefix[]={ "Hundred ","Thousand ","Million ","Billion ","Trillion "};
public static String Ans,op1,op2,op3;
public static long x,y;
public static int num1,num2,oneMillion=1000000,oneBillion=1000000000 ,zero=0;
public static void main(String args[]) throws IOException
{
String Num=" ";

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


while (Num.compareTo("")!=0)
{

op3="";
System.out.print("Enter a number: ");
Num=br.readLine();
System.out.print("\n");

Pattern pattern = Pattern.compile("\\s+");
Matcher matcher = pattern.matcher(Num);
boolean check = matcher.find();
String str = matcher.replaceAll("");
System.out.println();
Num=str;


if(Num.length()>14)
{
System.out.println("You exceeded the limit to be input.\n\nEnter only up to 14 digits.");
}
else if(Num.length()==0)
{
System.out.println("You entered nothing.\nTry Again!\n");
}

else if (Num.length()<=14)
{
System.out.println(NumtoWord(Num)+"\n");

num1=0;

for(num2=0; num2<= (Num.length()-1); num2++)
{
num1=num1+1;
if(num1==4)
{
num1=1;
op3=op3 +",";
}
op3=op3+Num.charAt((Num.length()-1)-num2);
}
Num="";
for(num2=0; num2<= (op3.length()-1); num2++)
{
Num=Num+op3.charAt((op3.length()-1)-num2);
}

System.out.print(Num + "\n\n");
}

}

}

public static String NumtoWord(String Str)
{

Ans="";
if (Str.length()<=9)
{

try
{


num2=Integer.parseInt(Str);
} catch(Exception e){};


/*0-0 */ if(num2==0)
{return "Zero";}
/*1-99 */ if(num2>=0 && num2<=99)
{Ans=BelowHundred(num2);}
/*100-999 */ else if(num2>=100 && num2<=999)
{Ans=Hundreds(num2);}
/*1000-99999 */ else if(num2>=1000 && num2<=99999)
{if ((num2/1000)!=0) {Ans=BelowHundred(num2/1000)+ prefix[1];}
Ans=Ans + Hundreds(num2 - ((num2/1000)*1000));}
/*100000-999999 */ else if(num2>=100000 && num2<=999999)
{if((num2/1000)!=0) { Ans=Hundreds(num2/1000)+ prefix[1];}
Ans=Ans + Hundreds(num2-((num2/1000)*1000));}
/*oneMillion-999999999 */ else if(num2>=oneMillion && num2<=999999999)
{if (num2<100000000) {Ans=BelowHundred(num2/oneMillion)+ prefix[2];}
else {Ans=Hundreds(num2/oneMillion)+ prefix[2];}
if(((num2 - ((num2/oneMillion) * oneMillion)) / 1000)>=1)
{Ans=Ans + Hundreds((num2 - ((num2/oneMillion) * oneMillion)) / 1000)+ prefix[1];}
Ans=Ans+ Hundreds((num2 - ((num2 / oneMillion) * oneMillion)) - ((num2 - ((num2 / oneMillion) * oneMillion)) / 1000 * 1000));}
}
else if(Str.length()>=10 && Str.length()<=12)
{ x=0;
/*oneBillion-999999999999 */
x=Long.valueOf(Str).longValue();
num1=Integer.parseInt(Long.toString(x/oneBillion));
if(num1!=0)
{if (num1<=99) {Ans=BelowHundred(num1)+ prefix[3];}
else if (num1>=100) {Ans=Hundreds(num1)+ prefix[3];}}

num1=Integer.parseInt(Long.toString(x-((x/oneBillion)*oneBillion)));
if (num1!=0)
{ if((num1/oneMillion)!=0)
{if (num1<100000000) {Ans=Ans+BelowHundred(num1/oneMillion)+ prefix[2];}
else {Ans=Ans+Hundreds(num1/oneMillion)+ prefix[2];}}
if (((num1 - ((num1/oneMillion) * oneMillion)) / 1000)!=0)
{
Ans=Ans + Hundreds((num1 - ((num1/oneMillion) * oneMillion)) / 1000)+ prefix[1];}
Ans=Ans + Hundreds((num1 - ((num1 / oneMillion) * oneMillion)) - ((num1 - ((num1 / oneMillion) * oneMillion)) / 1000 * 1000));

}
}

else if(Str.length()>=13 && Str.length()<=15)
{ x=0;

/*1000000000000-999999999999999 */
x=Long.valueOf(Str).longValue();

y=(x/oneBillion)/1000;
num1=Integer.parseInt(Long.toString(y));
if (num1!=0)
{if (num1<=99) {Ans=BelowHundred(num1)+ prefix[4];}
else if (num1>=100) {Ans=Hundreds(num1)+ prefix[4];}}

x= x-((y * oneBillion) * 1000);
if(x!=0)
{num1=Integer.parseInt(Long.toString(x/oneBillion));
if(num1!=0)
{if (num1<=99) {Ans=Ans+BelowHundred(num1)+ prefix[3];}
else if (num1>=100) {Ans=Ans+Hundreds(num1)+ prefix[3];}}

num1=Integer.parseInt(Long.toString(x-((x/oneBillion)*oneBillion)));
if (num1!=0)
{ if((num1/oneMillion)!=0)
{if (num1<100000000) {Ans=Ans+BelowHundred(num1/oneMillion)+ prefix[2];}
else {Ans=Ans+Hundreds(num1/oneMillion)+ prefix[2];}}
if (((num1 - ((num1/oneMillion) * oneMillion)) / 1000)!=0)
{Ans=Ans + Hundreds((num1 - ((num1/oneMillion) * oneMillion)) / 1000)+ prefix[1];}
Ans=Ans + Hundreds((num1 - ((num1 / oneMillion) * oneMillion)) - ((num1 - ((num1 / oneMillion) * oneMillion)) / 1000 * 1000));}
}
}



return(Ans);
}

//---------------------------------------------------------------------------------------------
public static String BelowHundred(int tNum)
{ op1="";
/*1-19 */ if(tNum>=1 && tNum<=19)
{op1=ones[tNum];}
/*20-99*/ else if(tNum>=20 && tNum<=99)
{op1=tens[(tNum/10)-2];
op1=op1 + ones[tNum -((tNum/10)*10)];}
return(op1);
}


/
public static String Hundreds(int xNum)
{ op2="";
/*100-999 */ if (xNum>=100)
{op2=ones[(xNum/100)] + prefix[0];
op2=op2 + BelowHundred(xNum- ((xNum/100)*100));}
else if (xNum<=99)
{op2=op2 + BelowHundred(xNum);}
return(op2) ;
}


}


I hope somebody can help me!!!

tnx
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-30-2008, 01:48 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Are there errors?
If none, then is this a logic problem? You can't figure out what the program is doing.
If the output is wrong, can you show give some examples of what it is doing incorrectly? Show the input and the output.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-30-2008, 02:15 AM
Member
 
Join Date: Aug 2008
Posts: 2
Rep Power: 0
fertas is on a distinguished road
Default reply:
actually this code runs smoothly. it already outputs the number to its equivalent words but i'm having a problem on how to put the decimal point. for example 345678 to 3,456.78 and should read as three thousand four hundred fifty six and seventy eight. while on this code when i enter the same output it prints as 345,678---three hundred fourty five thousand six hundred seventy eight
Note: the last two numbers should be taken as a decimal.

what should I do?

thanks,

Fertas
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-30-2008, 03:12 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
To convert 345678 to 3456.78 divide by 100.
To get the remainder use modulus:
int rem = num % 100;
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedding Word in an applet Java Tip SWT 0 07-25-2008 02:34 PM
Word OLE Java Tip SWT 0 07-25-2008 02:33 PM
Java Program To Convert A Number In To Words With Decimals javanewbie New To Java 1 07-02-2008 01:58 PM
get more decimals?!?! please help! michcio New To Java 7 05-22-2008 10:26 PM
Word Scramble lk9865 New To Java 5 11-17-2007 02:22 AM


All times are GMT +2. The time now is 09:42 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org