How To Get Number As Word In Java

12:59 AM | , , , ,

         Huft... this week is very busy guys. So I'm really sorry , if you waiting for me for a long. :-) Okay guys, for this time i will share you about my little experiment on java programming. I will show you about how to get a number as a words like a picture below.







           Are you curious? . Let's do it.  Okay for the first we will make a class to define a conversion of a number to ordinal number. You can see the source code below
===================================================

public class OrdinalNum {
public String getNumberInWords(int num){
String defaultNum="I'm so sorry, this program only show number between 0 - 1000";
if(num==1) return "One";
else if(num==2) return "Two";
else if(num==3) return "Three";
else if(num==4) return "Four";
else if(num==5) return "Five";
else if(num==6) return "Six";
else if(num==7) return "Seven";
else if(num==8) return "Eight";
else if(num==9) return "Nine";
else if(num==10) return "Ten";
else if(num==11) return "Eleven";
else if(num==12) return "Twelve";
else if(num==13) return "Thihrteen";
else if(num==13) return "Fourteen";
else if(num==15) return "Fifteen";
else if((num>=16)&&(num<=19)) return (getNumberInWords(num%10)+ "teen");
else if(num==20) return ("Twenty");
else if(num==30) return ("Thirty");
else if(num==50) return ("Fifty");
else if((num>=21)&&(num<=29))
return ("Twenty " +getNumberInWords(num%((num/10)*10)));
else if((num>=31)&&(num<=39))
return ("Thirty " +getNumberInWords(num%((num/10)*10)));
else if((num>=51)&&(num<=59))
return ("Fifty " +getNumberInWords(num%((num/10)*10)));
else if((num>=40)&&(num<=90)&&(num%10==0)) return (getNumberInWords(num/10) + "ty");
else if(((num>=41)&&(num<=49))||((num>=61)&&(num<=99)))
return (getNumberInWords(num/10) + "ty " +getNumberInWords(num%((num/10)*10)));
else if(num==100) return "One Hundred";
else if((num>=101)&&(num<=199)) return ( "One Hundred " + getNumberInWords(num%100));
else if((num>=200)&&(num<=900)&& (num%100==0)) return (getNumberInWords(num/100) + " Hundred");
else if((num>=201)&&(num<=999)) return (getNumberInWords(num/100) + " Hundred " + getNumberInWords(num%100));
else if(num==1000) return "One Thousand";
else if(num==0) return "Nol";
else return defaultNum;
}
public String getNumberInWordsDouble(String number){
number=number.replace(".", " ");
String [] a = number.split(" ");
if(a.length==1)
return getNumberInWords(Integer.parseInt(a[0]));
else
return getNumberInWords(Integer.parseInt(a[0])) + " Point " + getNumberInWords(Integer.parseInt(a[1]));
}
}
==================================================
       Okay guys, let's learning the code one by one. As you see , the OrdinalNumber class have two method. They are getNumberInWords() and getNumberInwordsDouble().The first method has return an integer number to ordinal number and the second one return a double number to ordinal number.
        I use the recursive function to get a value of number, example for get the value of 123. We define the number 1 as a hundred block, then we call the recursive function of getNumberInWords() for number 23 then we get twenty. After that we call the recursive function again for number 3. So we will get "One Hundred Twenty Three" for the value of 123.
       Okay, what about the floating point number?. In principle the same as the search integer value above. But we need to split the number before point (,) and the number after point. After that we call getNumberInWords function for the first segment and the second one like the code above.

       After that you can use the source code below to enter the number that you want to get the value.
 ==================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GetNumAsWord extends JFrame {
    private JTextField Text;
    private JLabel lbl;
    public GetNumAsWord(){
        super("Get Number As Words");
        Container container=getContentPane();
        container.setLayout(new FlowLayout());
        lbl=new JLabel("Enter number between 0 - 1000");
        Text=new JTextField(10);
        container.add(lbl);
        container.add(Text);
        TextFieldHandler handler=new TextFieldHandler();
        Text.addActionListener(handler);
        setLocationRelativeTo(null);
        setSize(400,200);
        setVisible(true);
    }
    public static void main(String[] args) {
        GetNumAsWord Aplikasi=new GetNumAsWord() ;
        Aplikasi.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    private class TextFieldHandler implements ActionListener {
        public void actionPerformed(ActionEvent event){
            String string="";
            double angka;
            int icon = 0,nomor;
            if(event.getSource()==Text){
                try{
                    OrdinalNum on=new OrdinalNum();
                    if(Text.getText().replace(",", ".").contains(".")){
                        angka=Double.parseDouble(Text.getText().replace(",", "."));
                        string=on.getNumberInWordsDouble(String.valueOf(angka));
                    }else{
                        nomor=Integer.parseInt(Text.getText().replace(",", "."));
                        string=on.getNumberInWords(nomor);
                    }
                    icon=1;
                }catch(Exception e){
                    string="Enter number between 0 - 1000";
                    icon=2;
                }finally{
                    JOptionPane.showMessageDialog(null, string, "Information", icon);
                }
            }
        }
    }
}
==================================================

Okay guys, let's try it. Yupz, i am very sorry that i just make this program to show the number between 0 until 1000, but you can modify this code to show more number. I think it's easy for you.... :)

Don't forget to comment this page , coz I just a newbie programmer... :)
I'm realy happy if  you want to be my friend. Add me on FB @ Alieer or my tweet @etno_indonesia.

0 comments:

Post a Comment

Please leave a comment