0

I use method NumberToWords() from this converting numbers in to words C# And I want to add all word to List. So I change code like this.

      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;

        }

And I want to add more string to List outside NumberToWords() such as

    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");

So I have to use List ttsEN = new List(); outside NumberToWords() . But when I use List ttsEN = new List(); outside NumberToWords() and run this code it show output like this which have 2 four and 2 five. It's wrong output.

four
four
thousand
five
five
hundred
and
fifteen

If I comment at ttsEN.Add(unitsMap[number]); it' work but not show fifteen after and in output.

I change to declare List like this outside NumberToWords()

List<string> ttsEN;

And use like this inside

ttsEN = new List<string>();

But it show output like this. How to use List outside method for Add more string ?

five
five  
hundred  
and  
fifteen
Community
  • 1
  • 1
user572575
  • 1,009
  • 3
  • 25
  • 45

1 Answers1

0

The code below will print five thousand and fifty-one [at] fifty [end]

But You should consider using StringBuilder for better perfomance. And do you really need List<string> in result? You can extract List from a string by splitting it by space char: List<string> list = result.Split(' ').ToList();

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;

    }
}
opewix
  • 4,993
  • 1
  • 20
  • 42