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,000,000,000 (one billion, short scale; one thousand million or one milliard, one yard, long scale) is the natural number following 999,999,999 and preceding 1,000,000,001. With a number, "billion" can be abbreviated as b, bil or bn. In standard form, it is written as 1 × 109.
Shelly will write the amount in figure as ₹ 20.50. The amount in words is written as twenty rupees and fifty paise.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With