C# Program to convert a given Number into Words
- Saturday, December 22, 2012, 11:47
- DotNet
- 25,496 views
In most of the application it is required to convert the number or amount in words. Following code will help you to convert a given number into words. For example, if “1234″ is given as input, output would be “One Thousand Two Hundred Thirty Four”.
This code will be convert any number between 1 to 9999 in words.
[csharp]
string[] Ones = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen" };
string[] Tens = { "Ten", "Twenty", "Thirty", "Fourty", "Fift", "Sixty", "Seventy", "Eighty", "Ninty" };
int no = int.Parse(txtNumber.Text);
string strWords = "";
if (no > 999 && no < 10000)
{
int i = no / 1000;
strWords = strWords + Ones[i – 1] + " Thousand ";
no = no % 1000;
}
if (no > 99 && no < 1000)
{
int i = no / 100;
strWords = strWords + Ones[i – 1] + " Hundred ";
no = no % 100;
}
if (no > 19 && no < 100)
{
int i = no / 10;
strWords = strWords + Tens[i – 1] + " ";
no = no % 10;
}
if (no > 0 && no < 20)
{
strWords = strWords + Ones[no – 1];
}
label2.Text = strWords;
[/csharp]