convert number into words
Asked Answered
E

7

8

even though this question has been posted and answered before. I wanted help with my code. Task is to convert a number into words from 0 to 10 million. I have tried to do that with my code using GUI, problem is it doesn't return an answer at all. Can anyone help me in identifying what the problem on the code could possibly be.

code below:

private void btnConvertToText_Click(object sender, EventArgs e)
    {
        string ConvertedNumber = " ";
        int number = Convert.ToInt32(txtNumber.Text);
        int Count = 0;

           string [] ones =  {" One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine "};
           string [] teens= {" Eleven ", " Twelve ", " Thirteen ", " Fourtte n ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen "};
           string [] tens = {" Ten ", " Twenty ", " Thirty ", " Fourty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninenty "};
           string [] moreThenTens = {" hundred ", " thousand ", " million "};

           while (number >= 0)
           {
               if (number == 0)
               {
                   ConvertedNumber = "Zero";
               }

              else if (number < 10)
               {
                   for (int Counter = 1; Counter < 10; Counter++)
                   {
                       ConvertedNumber += ones[Counter-1];
                   }
               }

               else if (number >= 10 && number < 100)
              {
                  while (number > 10)
                  {
                      if (number > 10 && number < 20)
                      {
                          for (int x = 11; x < 20; x++)
                          {
                              if (number == x)
                              {
                                  ConvertedNumber = teens[x - 11];
                              }
                          }
                      }

                      else
                      {
                          number -= 10;
                          Count++;
                      }

                      ConvertedNumber += tens[Count - 1];
                  }
              }

               else if (number >= 100 && number < 1000)
               {
                   while (number > 100)
                   {
                       number -= 100;
                       Count++;
                   }
                   ConvertedNumber += ones[Count - 1] + moreThenTens[0];
               }

               else if (number >= 1000 && number < 10000)
               {
                   while (number > 1000)
                   {
                       number -= 1000;
                       Count++;
                   }

                   ConvertedNumber += ones[Count - 1] + moreThenTens[1];
               }

               else if (number >= 10000 && number < 100000)
               {
                   while (number > 10000)
                   {
                       if (number < 20000)
                       {
                           number -= 10000;
                           Count++;
                       }

                       else if (number >= 20000)
                       {
                           number -= 10000;
                           Count++;
                       }

                   }

                   if (Count >= 1)
                   {
                       if (number < 1000)
                       {
                           ConvertedNumber += tens[0] + moreThenTens[1];
                       }

                       else if (number >= 1000)
                       {
                           ConvertedNumber += teens[Count - 1] + moreThenTens[1];
                       }
                   }

                   else if (Count > 1)
                   {
                       ConvertedNumber += tens[Count - 1] + moreThenTens[1];
                   }
               }

               else if (number >= 100000 && number < 1000000)
               {
                   while (number > 100000)
                   {
                       number -= 100000;
                       Count++;
                   }

                   ConvertedNumber += ones[Count - 1] + moreThenTens[0] + moreThenTens[1];
               }

               else if (number >= 1000000 && number < 10000000)
               {
                   while (number > 1000000)
                   {
                       number -= 1000000;
                       Count++;
                   }

                   ConvertedNumber += ones[Count - 1] + moreThenTens[2];
               }

               else if (number == 10000000)
               {
                   while (number > 10000000)
                   {
                       number -= 10000000;
                       Count++;
                   }

                   ConvertedNumber += tens[0] + moreThenTens[2];
               }
           }

           txtConvertedNumber.Text = ConvertedNumber;
    }
}
Ernieernst answered 4/8, 2013 at 19:38 Comment(3)
possible duplicate of converting numbers in to words C#Parturifacient
Did you step through the code with a debugger?Nonproductive
I think this is blackbeltcoder.com/Articles/strings/converting-numbers-to-words what you want.Skepful
P
5
while (number >= 0)

It seems like your code is creating an infinite loop. Take for example:

if (number == 0)
{
    ConvertedNumber = "Zero";
}

This does not quit your loop, and therefore never arrives at

txtConvertedNumber.Text = ConvertedNumber;

Use a check to convert your number, like:

if (number == 0)
{
    ConvertedNumber = "Zero";
    number = -1;
}
Provinciality answered 4/8, 2013 at 20:6 Comment(2)
I guess for every number it'll give the answer to be zero!Glassman
Not if you include the correct checks and ensure that if the number goes to zero somewhere else it will automatically become -1 instead of 0Provinciality
C
4

This might be useful

public static string NumberToWords(int number)
    {
        if (number == 0)
            return "Zero";

        if (number < 0)
            return "Minus " + NumberToWords(Math.Abs(number));

        string words = "";

        if ((number / 1000000) > 0)
        {
            words += NumberToWords(number / 1000000) + " Million ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += NumberToWords(number / 1000) + " Thousand ";
            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " Hundred ";
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += "and ";

            var unitsMap = new[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            var tensMap = new[] { "zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "-" + unitsMap[number % 10];
            }
        }

        return words;
    }
Cottonmouth answered 5/8, 2013 at 5:15 Comment(0)
G
1

You should check if the number is zero before the while loop

 if ( number == 0 )
      ConvertedNumber= "Zero" ;

Then modify the while as follows

while(number > 0)
Glassman answered 4/8, 2013 at 20:18 Comment(0)
H
1

this may help for decimal number

string[] number = Number1.ToString().Split('.');

String Word=NumberToWords(convert.toInt64(number[0])) + " AND " +     NumberToWords(convert.toInt64(number[1]));
Hufnagel answered 7/5, 2014 at 10:38 Comment(1)
How is that at all an answer to this question?Gynarchy
V
0

seems like it's stuck in loop.. i checked teen number part here, seems like you forgot to decrement the number to less than zero or zero

                else if (number >= 10 && number < 100)
              {
              if(number > 10)
              {
                  if (number > 10 && number < 20)
                  {
                      for (int x = 11; x < 20; x++)
                      {
                          if (number == x)
                          {
                              ConvertedNumber += teens[x - 11];

                          }
                      }
                      ***number -= 100;***
                  }

                  else
                  {
                      number -= 10;
                      Count++;
                      ConvertedNumber += tens[Count - 1];
                  }


              }
          }
Vento answered 4/8, 2013 at 20:26 Comment(0)
E
0

This would also work and make your code easier to debug / follow.

while (NumberExists(ConvertedNumber, number))
{
   ...
}

Place this method in the same nameSpace as btnConvertToText_Click.

    private static bool NumberExists(string convertedNumber, int number)
    {
        if (convertedNumber == " " && number != null)
        {
            return true;
        }
        else
            return false;
    }

In general I'd suggest not adding magic numbers, like +1s or -1s to your code. It can make debugging a bit more of a chore. If you have to do it to get something to work try to figure out why and rewrite it ASAP. Definitely before working on other methods.

Expellee answered 4/8, 2013 at 20:35 Comment(0)
A
0
    class Program
    {
        public static string DecimalToText(string decimalPart)
        {
            string[] digits = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
            string result = "";
            foreach (char c in decimalPart)
            {
                int i = (int)c - 48;
                if (i < 0 || i > 9) return ""; // invalid number, don't return anything
                result += " " + digits[i];
            }
            return result;
        }

        public static string NumberToText(int number, bool useAnd)
        {
            if (number == 0) return "Zero";



            if (number == -2147483648) return "Minus Two Hundred " + "and " + "Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred " + "and " + "Forty Eight";

            int[] num = new int[4];
            int first = 0;
            int u, h, t;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (number < 0)
            {
                sb.Append("Minus ");
                number = -number;
            }
            string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
            string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
            string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
            string[] words3 = { "Thousand ", "Lakh ", "Crore " };
            num[0] = number % 1000; // units
            num[1] = number / 1000;
            num[2] = number / 100000;
            num[1] = num[1] - 100 * num[2]; // thousands
            num[3] = number / 10000000; // crores
            num[2] = num[2] - 100 * num[3]; // lakhs
            for (int i = 3; i > 0; i--)
            {
                if (num[i] != 0)
                {
                    first = i;
                    break;
                }
            }

            for (int i = first; i >= 0; i--)
            {
                if (num[i] == 0) continue;

                u = num[i] % 10; // ones 
                t = num[i] / 10;
                h = num[i] / 100; // hundreds
                t = t - 10 * h; // tens

                if (h > 0) sb.Append(words0[h] + "Hundred ");
                if (u > 0 || t > 0)
                {
                    if (h > 0 || i < first) sb.Append("and ");

                    if (t == 0)
                        sb.Append(words0[u]);
                    else if (t == 1)
                        sb.Append(words1[u]);
                    else
                        sb.Append(words2[t - 2] + words0[u]);
                }
                if (i != 0) sb.Append(words3[i - 1]);
            }

            string temp = sb.ToString().TrimEnd();


            return temp;
        }

        static void Main(string[] args)
        {
            double d = 1234.56;
            int i = (int)d;
            string decimalPart = d.ToString().Split('.')[1];
            string text = NumberToText(i, true) + " Point" + DecimalToText(decimalPart);
            Console.WriteLine(text);
            Console.ReadKey();
        }
    }
Archdiocese answered 28/3, 2020 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.