将数字转换为单词

时间:2013-08-04 19:38:19

标签: c# numbers word

即使此问题已经发布并在之前得到解答。我想要帮助我的代码。任务是将数字转换为0到1000万的单词。我试图用我的代码使用GUI来做到这一点,问题是它根本没有返回答案。任何人都可以帮助我确定代码可能存在的问题。

以下代码:

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

7 个答案:

答案 0 :(得分:5)

while (number >= 0)

好像你的代码正在创建一个无限循环。举个例子:

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

这不会退出你的循环,因此永远不会到达

txtConvertedNumber.Text = ConvertedNumber;

使用支票转换您的号码,例如:

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

答案 1 :(得分:2)

这可能很有用

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

答案 2 :(得分:1)

你应该在while循环之前检查数字是否为零

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

然后按如下所示修改while

while(number > 0)

答案 3 :(得分:1)

这可能有助于十进制数

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

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

答案 4 :(得分:0)

似乎它被困在循环中..我在这里检查了青少年数字部分,似乎  就像你忘了将数字减少到小于零或零

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


              }
          }

答案 5 :(得分:0)

这也可以使您的代码更易于调试/跟踪。

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

将此方法放在与nameSpace相同的btnConvertToText_Click

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

一般情况下,我建议不要在代码中添加幻数,例如+ 1s或-1s。它可以使调试更多的苦差事。如果你必须这样做以获得一些工作,试着找出原因并尽快重写。绝对在开始其他方法之前。

答案 6 :(得分: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();
        }
    }