Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
if (textAmount.Text != "")
LabelNoToWord.Text = (changeNumericToWords(Convert.ToInt32(textAmount.Text)));

        }
        public String changeNumericToWords(double numb)
        {
            String num = numb.ToString();
            return changeToWords(num, false);
        }
        public String changeCurrencyToWords(String numb)
        {
            return changeToWords(numb, true);
        }
        public String changeNumericToWords(String numb)
        {
            return changeToWords(numb, false);
        }
        public String changeCurrencyToWords(double numb)
        {
            return changeToWords(numb.ToString(), true);
        }
        public String changeToWords(String numb, bool isCurrency)
        {
            String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
            String endStr = (isCurrency) ? ("Only") : ("");
            try
            {
                int decimalPlace = numb.IndexOf(".");
                if (decimalPlace > 0)
                {
                    wholeNo = numb.Substring(0, decimalPlace);
                    points = numb.Substring(decimalPlace + 1);
                    if (Convert.ToInt32(points) > 0)
                    {
                        andStr = (isCurrency) ? ("and") : ("point");// just to separate whole numbers from points/cents
                        endStr = (isCurrency) ? ("Cents " + endStr) : ("");
                        pointStr = translateCents(points);
                    }
                }
                val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
            }
            catch { ;}
            return val;
        }
        private String translateWholeNumber(String number)
        {
            string word = "";
            try
            {
                bool beginsZero = false;//tests for 0XX
                bool isDone = false;//test if already translated
                double dblAmt = (Convert.ToDouble(number));
                //if ((dblAmt > 0) && number.StartsWith("0"))
                if (dblAmt > 0)
                {//test for zero or digit zero in a nuemric
                    beginsZero = number.StartsWith("0");

                    int numDigits = number.Length;
                    int pos = 0;//store digit grouping
                    String place = "";//digit grouping name:hundres,thousand,etc...
                    switch (numDigits)
                    {
                        case 1://ones' range
                            word = ones(number);
                            isDone = true;
                            break;
                        case 2://tens' range
                            word = tens(number);
                            isDone = true;
                            break;
                        case 3://hundreds' range
                            pos = (numDigits % 3) + 1;
                            place = " Hundred ";
                            break;
                        case 4://thousands' range
                        case 5:
                        case 6:
                            pos = (numDigits % 4) + 1;
                            place = " Thousand ";
                            break;
                        case 7://millions' range
                        case 8:
                        case 9:
                            pos = (numDigits % 7) + 1;
                            place = " Million ";
                            break;
                        case 10://Billions's range
                            pos = (numDigits % 10) + 1;
                            place = " Billion ";
                            break;
                        //add extra case options for anything above Billion...
                        default:
                            isDone = true;
                            break;
                    }
                    if (!isDone)
                    {//if transalation is not done, continue...(Recursion comes in now!!)
                        word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
                        //check for trailing zeros
                        if (beginsZero) word = " and " + word.Trim();
                    }
                    //ignore digit grouping names
                    if (word.Trim().Equals(place.Trim())) word = "";
                }
            }
            catch { ;}
            return word.Trim();
        }
        private String tens(String digit)
        {
            int digt = Convert.ToInt32(digit);
            String name = null;
            switch (digt)
            {
                case 10:
                    name = "Ten";
                    break;
                case 11:
                    name = "Eleven";
                    break;
                case 12:
                    name = "Twelve";
                    break;
                case 13:
                    name = "Thirteen";
                    break;
                case 14:
                    name = "Fourteen";
                    break;
                case 15:
                    name = "Fifteen";
                    break;
                case 16:
                    name = "Sixteen";
                    break;
                case 17:
                    name = "Seventeen";
                    break;
                case 18:
                    name = "Eighteen";
                    break;
                case 19:
                    name = "Nineteen";
                    break;
                case 20:
                    name = "Twenty";
                    break;
                case 30:
                    name = "Thirty";
                    break;
                case 40:
                    name = "Fourty";
                    break;
                case 50:
                    name = "Fifty";
                    break;
                case 60:
                    name = "Sixty";
                    break;
                case 70:
                    name = "Seventy";
                    break;
                case 80:
                    name = "Eighty";
                    break;
                case 90:
                    name = "Ninety";
                    break;
                default:
                    if (digt > 0)
                    {
                        name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));
                    }
                    break;
            }
            return name;
        }
        private String ones(String digit)
        {
            int digt = Convert.ToInt32(digit);
            String name = "";
            switch (digt)
            {
                case 1:
                    name = "One";
                    break;
                case 2:
                    name = "Two";
                    break;
                case 3:
                    name = "Three";
                    break;
                case 4:
                    name = "Four";
                    break;
                case 5:
                    name = "Five";
                    break;
                case 6:
                    name = "Six";
                    break;
                case 7:
                    name = "Seven";
                    break;
                case 8:
                    name = "Eight";
                    break;
                case 9:
                    name = "Nine";
                    break;
            }
            return name;
        }
        private String translateCents(String cents)
        {
            String cts = "", digit = "", engOne = "";
            for (int i = 0; i < cents.Length; i++)
            {
                digit = cents[i].ToString();
                if (digit.Equals("0"))
                {
                    engOne = "Zero";
                }
                else
                {
                    engOne = ones(digit);
                }
                cts += " " + engOne;
            }
            return cts;
        }


What I have tried:

like 1110.56 in word like, One Thousand One Hundred Ten Rupees And Fifty Six Paise onyl.

i m making a code but if i inter dot then program is stop working and error
Posted
Updated 17-Jul-22 1:50am
v2
Comments
BillWoodruff 29-Jun-16 4:49am    
No Lakhs ? No Crores ?

So use the debugger: put a breakpoint on the first line in changeToWords and run your code.
When the debugger reaches it, it will stop and let you take control. You can use the mouse (and various debug panes) to look at the variable contents. Step though your code working out what you expect to happen before you execute each line, and compare that with what did actually happen. Did it do what you expected? OK - move on. Otherwise, why not? What did it do that you didn't expect, or not do that you did?
This is a skill - predictably called debugging - and the only way to develop it is to use it. And it's a lot simpler to develop a skill on a simple project like this than on a 100,000 line monster with a subtle bug...

So give it a go. See what you can find out.
 
Share this answer
 
Comments
Maciej Los 29-Jun-16 1:46am    
5ed!
Why to force doors wide open?

There's a lot of articles about that. Check this[^].

You may be most interested in: Number To Word (Arabic Version)[^]

As OriginalGriff[^] mentioned, the best way to find an error in your code is to use debugger.
 
Share this answer
 
<pre lang="Java">import java.util.*;

public class demo {
    public static void main(String args[] ) throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        Scanner sc = new Scanner(System.in);
        int input1 = sc.nextInt();
        String result = "";
        if(input1==1){
            int n = sc.nextInt();
            result = result + solution.lakh(n) + solution.thousand(n) + solution.hundred(n);
        }
        if(input1==2){
            int n = sc.nextInt();
            result = result + solution.million(n) + solution.thousand2(n) + solution.hundred(n);
        }
        System.out.println(result);
        sc.close();
    }
}
class solution{
    public static String hundred(int n){
        HashMap<Integer, String> words = new HashMap<>();
        words.put(0, "");
        words.put(1, "ONE");
        words.put(2, "TWO");
        words.put(3, "THREE");
        words.put(4, "FOUR");
        words.put(5, "FIVE");
        words.put(6, "SIX");
        words.put(7, "SEVEN");
        words.put(8, "EIGHT");
        words.put(9, "NINE");
        words.put(10, "TEN");
        words.put(11, "ELEVEN");
        words.put(12, "TWELVE");
        words.put(13, "THIRTEEN");
        words.put(14, "FOURTEEN");
        words.put(15, "FIFTEEN");
        words.put(16, "SIXTEEN");
        words.put(17, "SEVENTEEN");
        words.put(18, "EIGHTEEN");
        words.put(19, "NINETEEN");
        words.put(20, "TWENTY");
        words.put(30, "THIRTY");
        words.put(40, "FORTY");
        words.put(50, "FIFTY");
        words.put(60, "SIXTY");
        words.put(70, "SEVENTY");
        words.put(80, "EIGHTY");
        words.put(90, "NINETY");
        n = n%1000;
        String result="";
        if(n==0){
            result="";
        }
        else{
            if(n/100!=0){
                result = result + words.get(n/100) + " HUNDRED ";
                if(n%100<20){
                    result = result + words.get(n%100) + " ";
                }
                else{
                    result = result + words.get(((n%100)/10)*10) + " " + words.get(((n%100)%10)) + " ";
                }
            }
            else {
                if(n/10!=0){
                    if(n%10==0){
                        result = result + words.get((n/10)*10) + " ";
                    }
                    else{
                        if(n<20){
                            result = result + words.get(n) + " ";
                        }
                        else{
                            result = result + words.get((n/10)*10) + " " + words.get(n%10) + " ";
                        }
                    }
                }
                else{
                    result = words.get(n) + " ";
                }
            }
        }
        return result;
    }
    public static String thousand(int n){
        HashMap<Integer, String> words = new HashMap<>();
        words.put(0, "");
        words.put(1, "ONE");
        words.put(2, "TWO");
        words.put(3, "THREE");
        words.put(4, "FOUR");
        words.put(5, "FIVE");
        words.put(6, "SIX");
        words.put(7, "SEVEN");
        words.put(8, "EIGHT");
        words.put(9, "NINE");
        words.put(10, "TEN");
        words.put(11, "ELEVEN");
        words.put(12, "TWELVE");
        words.put(13, "THIRTEEN");
        words.put(14, "FOURTEEN");
        words.put(15, "FIFTEEN");
        words.put(16, "SIXTEEN");
        words.put(17, "SEVENTEEN");
        words.put(18, "EIGHTEEN");
        words.put(19, "NINETEEN");
        words.put(20, "TWENTY");
        words.put(30, "THIRTY");
        words.put(40, "FORTY");
        words.put(50, "FIFTY");
        words.put(60, "SIXTY");
        words.put(70, "SEVENTY");
        words.put(80, "EIGHTY");
        words.put(90, "NINETY");
        n = (n/1000)%100;
        String result = "";
        if(n==0){
            result = "";
        }
        else{
        if(n%10==0){
            result = result + words.get(n) + " THOUSAND ";
        }
        else{
            if(n<20){
                result = result + words.get(n) + " THOUSAND ";
            }
            else{
                result = result + words.get((n/10)*10) + " " + words.get(n%10) + " THOUSAND ";
            }
        }
    }
        return result;
    }
    public static String lakh(int n){
        HashMap<Integer, String> words = new HashMap<>();
        words.put(0, "");
        words.put(1, "ONE");
        words.put(2, "TWO");
        words.put(3, "THREE");
        words.put(4, "FOUR");
        words.put(5, "FIVE");
        words.put(6, "SIX");
        words.put(7, "SEVEN");
        words.put(8, "EIGHT");
        words.put(9, "NINE");
        words.put(10, "TEN");
        words.put(11, "ELEVEN");
        words.put(12, "TWELVE");
        words.put(13, "THIRTEEN");
        words.put(14, "FOURTEEN");
        words.put(15, "FIFTEEN");
        words.put(16, "SIXTEEN");
        words.put(17, "SEVENTEEN");
        words.put(18, "EIGHTEEN");
        words.put(19, "NINETEEN");
        words.put(20, "TWENTY");
        words.put(30, "THIRTY");
        words.put(40, "FORTY");
        words.put(50, "FIFTY");
        words.put(60, "SIXTY");
        words.put(70, "SEVENTY");
        words.put(80, "EIGHTY");
        words.put(90, "NINETY");
        n = n/100000;
        String result = "";
        if(n==0){
            result = "";
        }
        else{
        if(n/10==0){
            result = result + words.get(n) + " LAKH ";
        }
        else{
            if(n<20){
                result = result + words.get(n) + " LAKH ";
            }
            else{
                result = result + words.get((n/10)*10) + " " + words.get(n%10) + " LAKH ";
            }
        }
    }
        return result;
    }

    public static String thousand2(int n){
        n = (n/1000)%1000;
        String result = "";
        if(n==0){
            result = "";
        }
        else{
            result = result + solution.hundred(n) + "THOUSAND ";
        }
        return result;
    }
    public static String million(int n){
        n = n/1000000;
        String result = "";
        if(n==0){
            result = "";
        }
        else{
            result = result + solution.hundred(n) + "MILLION ";
        }
        return result;
    }
}
 
Share this answer
 
Comments
Dave Kreskowiak 17-Jul-22 12:46pm    
Doing someones homework assignment for them, and this is assigned very frequently, is never a good idea.

You just helped anyone who uses your code and turns it in as their own work to fail the assignment for plagiarism.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900