convert number into words

拟墨画扇 提交于 2019-12-22 05:02:20

问题


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;
    }
}

回答1:


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;
}



回答2:


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;
    }



回答3:


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)



回答4:


this may help for decimal number

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

String Word=NumberToWords(convert.toInt64(number[0])) + " AND " +     NumberToWords(convert.toInt64(number[1]));



回答5:


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.




回答6:


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];
                  }


              }
          }


来源:https://stackoverflow.com/questions/18046836/convert-number-into-words

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!