Convert Numbers to Words | Convert Digits to Figures in Java

Q.: Write a java program to convert amount from digit to figures.

OR

Q. Write a java program to convert numbers to words

OR

Q. Write a java program to convert digits to figures.

Compatibility/Version: Java 2 Platform Standard Edition Version 1.5 (Java 2 SDK, SE v1.5.0)

Code:

import java.util.*;
public class notowords
{
    private static String amt;
	private static int num;
    private static String[] units={""," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
    private static String[] teen={" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
    private static String[] tens={" Twenty"," Thirty"," Fourty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
    private static String[] maxs={"",""," Hundred"," Thousand"," Lakh"," Crore"};
    public notowords()
    {
        amt="";
    }
    public String convToWord(int n)
    {
        amt=noToStr(n);
		String conv="";
		int pos=1;
		boolean hun=false;
        while(amt.length()>0)
        {
            if(pos==1)
            {
				if(amt.length()>=2)
                {
					String C=amt.substring(amt.length()-2,amt.length());
					amt=amt.substring(0,amt.length()-2);
					conv+=digits(C);
				}
                else if(amt.length()==1)
                {
					conv+=digits(amt);
					amt="";
				}
				pos++;
            }
            else if(pos==2)
            {
				String C=amt.substring(amt.length()-1,amt.length());
				amt=amt.substring(0,amt.length()-1);
                if(conv.length()>0 && digits(C)!="")
				{
					conv=(digits(C)+maxs[pos]+" and")+conv;
					hun=true;
				}
				else
				{
					if(digits(C)=="");
					else
						conv=(digits(C)+maxs[pos])+conv;
						hun=true;
				}
				pos++;
            }
            else if(pos>2)
            {
                if(amt.length()>=2)
                {
					String C=amt.substring(amt.length()-2,amt.length());
					amt=amt.substring(0,amt.length()-2);
                    if(!hun && conv.length()>0)
						conv=digits(C)+maxs[pos]+" and"+conv;
					else
					{
						if(digits(C)=="");
						else conv=digits(C)+maxs[pos]+conv;
					}
				}
                else if(amt.length()==1)
                {
					if(!hun && conv.length()>0)conv=digits(amt)+maxs[pos]+" and"+conv;
					else
					{
						if(digits(amt)=="");
						else
							conv=digits(amt)+maxs[pos]+conv;
							amt="";
					}
				}
				pos++;
            }
        }
        return conv;
    }

    private String digits(String C)
    {
        String conv="";
        for(int i=C.length()-1;i>=0;i--)
        {
			int ch=C.charAt(i)-48;
            if(i==0 && ch>1 && C.length()>1)
				conv=tens[ch-2]+conv;
            else if(i==0 && ch==1 && C.length()==2)
            {
				int sum=0;
				for(int j=0;j<2;j++)
					sum=(sum*10)+(C.charAt(j)-48);
					return teen[sum-10];
			}
            else
			{
				if(ch>0)conv=units[ch]+conv;
			}
        }
		return conv;
    }

    private String noToStr(int n)
    {
        String num="";
		while(n!=0)
		{
			num=((char)((n%10)+48))+num;
			n/=10;
		}
		return num;
    }

    private void inp()
    {
        Scanner in=new Scanner(System.in);
        try
		{
			System.out.print("Enter Amount in Digits/Numbers : ");
			num=in.nextInt();
		}
		catch(Exception e)
		{
			System.out.println("Number should be less than 1 Arab(1,00,00,00,000) !");
			System.exit(1);
		}
    }

    public static void main(String arg[])
    {
        notowords obj=new notowords();
        obj.inp();
        System.out.println("Amount in Words/Figures : "+obj.convToWord(num) + " !");
    }
}

Output:

Digits to Figures

Fig.: Convert from Digits to Figures in Java

Download Now (.Zip File – 16KB)…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.