locked
hundreds for Number to Word RRS feed

  • Question

  • Hi,

    I am using below class to convert from number to words but the 196.500 is converted to:

    One Hundred Ninety Six BD and Five Fils Only

    where it should be:

    One Hundred Ninety Six BD and Five Hundred Fils Only

    How can I fix this please?

    this is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Application_Class_Library;
    
    namespace Print_A_Check
    {
        public class number_to_words
        {
            public class NumberToEnglish
            {
                Application_Class_Library.data_server serverClass = new Application_Class_Library.data_server();
    
                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);
                }
                private 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);
    
                                andStr = (isCurrency) ? (serverClass.read_value("Print-A-Check", "Print-A-Check", "CurrencyName") + " and") : ("point");// just to separate whole numbers from points/cents
                                endStr = (isCurrency) ? (serverClass.read_value("Print-A-Check", "Print-A-Check", "DecimalName") + " " + 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;
                }
            }
        }
    }
    

    Thanks,

    Jassim

    Friday, October 7, 2016 8:41 PM

Answers

  • Try the following way of conversions:

      string num = numb.ToString("F3");


    By the way, consider the decimal type too, instead of double.

    Saturday, October 8, 2016 8:53 AM
  • Hi Jassim,

    I agree with Viorel. I have tested your code on my side, your code should be like this.

    public String changeCurrencyToWords(double numb)
                {
                    return changeToWords(numb.ToString("F3"), true);
                }

    >>where it should be:

    >>One Hundred Ninety SixBD and Five Hundred Fils Only

    Based on your scenario, the result as above is wrong, since the following code make a branch after the decimal point, if you change code as Viorel_ posted,

     wholeNo = numb.Substring(0, decimalPlace);
      points = numb.Substring(decimalPlace + 1);
      

    The test result One Hundred Ninety Six  Five Zero Zero  should be right but not as your expected.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 11, 2016 3:24 AM

All replies

  • Try the following way of conversions:

      string num = numb.ToString("F3");


    By the way, consider the decimal type too, instead of double.

    Saturday, October 8, 2016 8:53 AM
  • I am using:

    NumberToWords.changeCurrencyToWords(Convert.ToDouble(my_number);

    How and where should I set the numb.ToString("F3")?


    Thanks,

    Jassim

    Saturday, October 8, 2016 8:23 PM
  • Hi Jassim,

    I agree with Viorel. I have tested your code on my side, your code should be like this.

    public String changeCurrencyToWords(double numb)
                {
                    return changeToWords(numb.ToString("F3"), true);
                }

    >>where it should be:

    >>One Hundred Ninety SixBD and Five Hundred Fils Only

    Based on your scenario, the result as above is wrong, since the following code make a branch after the decimal point, if you change code as Viorel_ posted,

     wholeNo = numb.Substring(0, decimalPlace);
      points = numb.Substring(decimalPlace + 1);
      

    The test result One Hundred Ninety Six  Five Zero Zero  should be right but not as your expected.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 11, 2016 3:24 AM