将数字转换为单词无法向List外部方法添加单词

时间:2014-12-20 04:27:54

标签: c# visual-studio-2010 word

我使用此converting numbers in to words C#中的方法NumberToWords()并且我想将所有单词添加到List。所以我改变了这样的代码。

      public List<String> NumberToWords(int number)
        {

            /* It will show wrong output when declare outside */
            List<string> ttsEN = new List<string>();  

            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 == 0)
            {
                ttsEN.Add("zero");
                return ttsEN;
            }
            string words = "";

            if ((number / 1000000) > 0)
            {
                words += NumberToWords(number / 1000000) + " million ";

                ttsEN.Add(unitsMap[(number / 1000000)]);
                ttsEN.Add("million");

                number %= 1000000;
            }

            if ((number / 1000) > 0)
            {
                words += NumberToWords(number / 1000) + " thousand ";

                ttsEN.Add(unitsMap[(number / 1000)]);
                ttsEN.Add("thousand");

                number %= 1000;
            }

            if ((number / 100) > 0)
            {
                words += NumberToWords(number / 100) + " hundred ";

                ttsEN.Add(unitsMap[(number / 100)]);
                ttsEN.Add("hundred");

                number %= 100;
            }

            if (number > 0)
            {
                if (words != "")
                {
                    words += "and ";
                    ttsEN.Add("and");
                }

                if (number < 20)
                {
                    words += unitsMap[number];

/* If comment this line it's work but not show fifteen after and in output */
                    ttsEN.Add(unitsMap[number]);  

                }
                else
                {
                    words += tensMap[number / 10];
                    ttsEN.Add(tensMap[number / 10]);

                    if ((number % 10) > 0)
                    {
                        words += "-" + unitsMap[number % 10];
                        ttsEN.Add(unitsMap[number % 10]);
                    }
                }

            }
            return ttsEN;

        }

我想在NumberToWords()之外的List中添加更多字符串,例如

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> voiceEN = new List<string>();
        voiceEN = NumberToWords("5051");
        ttsEN.Add("at");
        voiceEN = NumberToWords("50");
        ttsEN.Add("end");

所以我必须在NumberToWords()之外使用 List ttsEN = new List(); 。 但是当我在NumberToWords()之外使用 List ttsEN = new List(); 并运行此代码时,它显示的输出有2个4和2个5。这是错误的输出。

four
four
thousand
five
five
hundred
and
fifteen

如果我在 ttsEN.Add(unitsMap [number])发表评论它&#39;在输出中之后,但不显示十五

我更改为在NumberToWords()

之外声明List
List<string> ttsEN;

并在内部使用

ttsEN = new List<string>();

但它会显示这样的输出。如何使用List outside方法添加更多字符串?

five
five  
hundred  
and  
fifteen

1 个答案:

答案 0 :(得分:0)

以下代码将打印five thousand and fifty-one [at] fifty [end]

<强>但是 您应该考虑使用StringBuilder来获得更好的性能。你真的需要List<string>的结果吗?您可以通过用空格char List<string> list = result.Split(' ').ToList();

拆分来从字符串中提取List
class Program
{
    static void Main(string[] args)
    {
        string line = string.Empty;
        line += NumberToWords(5051);
        line += " [at] ";
        line += NumberToWords(50);
        line += " [end]";

        Console.WriteLine(line);
        Console.ReadKey();
    }

    public static string NumberToWords(int number)
    {
        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 == 0)
        {
            return "zero";
        }

        string words = string.Empty;

        if ((number / 1000000) > 0)
        {
            words += unitsMap[(number / 1000000)];
            words += " million";

            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += unitsMap[(number / 1000)];
            words += " thousand";

            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += unitsMap[(number / 100)];
            words += " hundred";

            number %= 100;
        }

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

            if (number < 20)
            {
                words += unitsMap[number];
            }
            else
            {
                words += tensMap[number / 10];

                if ((number % 10) > 0)
                {
                    words += "-" + unitsMap[number % 10];
                }
            }

        }
        return words;

    }
}