# number to word with decimals

• 08-30-2008, 01:30 AM
fertas
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=" ";

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

op3="";
System.out.print("Enter a number: ");
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
• 08-30-2008, 01:48 AM
Norm
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.
• 08-30-2008, 02:15 AM
fertas