Results 1 to 4 of 4
  1. #1
    fertas is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    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.

  3. #3
    fertas is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    To convert 345678 to 3456.78 divide by 100.
    To get the remainder use modulus:
    int rem = num % 100;

Similar Threads

  1. Embedding Word in an applet
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:34 PM
  2. Word OLE
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:33 PM
  3. Replies: 1
    Last Post: 07-02-2008, 02:58 PM
  4. get more decimals?!?! please help!
    By michcio in forum New To Java
    Replies: 7
    Last Post: 05-22-2008, 11:26 PM
  5. Word Scramble
    By lk9865 in forum New To Java
    Replies: 5
    Last Post: 11-17-2007, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •