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