Introduction
Usually every developer come across the requirement of writing the number into words, especially when developing accounts, payroll softwares etc.
Here is the small code that generates the number into words. This is limited upto 10000 only. You can extend this upto your requirement
1. Place two textboxes TextBox1 ,TextBox2 and a Button1 on to Webform
2. Copy the code and paste into button1_click event.
private void button1_Click(object sender, EventArgs e) { int n = int.Parse(textBox1.Text); int i = 0; string[] ones = new string[] {"one","two", "three","four","five","six","seven" ,"eight","nine","ten" , "eleven","tweleve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"}; string[] tens = new string[] { "ten ", "twenty ", "thirty ", "fourty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninty " }; string s = ""; if (n > 999 && n < 10000) { i = n / 1000; s = ones[i - 1] + " thousand " ; n = n % 1000; } if (n > 99 && n < 1000) { i = n / 100; s =s+ ones[i - 1] + " hundred "; n = n % 100; } if (n > 19 && n < 100) { i = n / 10; s = s + tens[i - 1]; n = n % 10; } if (n > 0 && n < 20) { s = s + ones[n- 1]; } textBox2.Text = s; }
Enter any number below 10000 in TextBox1 and click on Button1, which gives the result in TextBox2
Like this way, one can extend this code to their requirement
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment