Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
C#
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick_Click(object sender, EventArgs e)
{
string word = ConvertNumbertoWords(Convert.ToInt32(txtnumber.Text));
lblmsg.InnerText = word;
}
public static string ConvertNumbertoWords(int number)
{
if (number == 0)
return "ZERO";
if (number < 0)
return "minus " + ConvertNumbertoWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += ConvertNumbertoWords(number / 1000000) + " MILLION ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += ConvertNumbertoWords(number / 1000) + " THOUSAND ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += ConvertNumbertoWords(number / 100) + " HUNDRED ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "AND ";
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 < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += " " + unitsMap[number % 10];
}

}
return words;
}
}

I am Able to do the task with the button but I want to It without using The button.
If is there any way the please tell me
Posted
Comments
syed shanu 9-Dec-14 2:56am    
protected void Page_Load(object sender, EventArgs e)
{
string word = ConvertNumbertoWords(Convert.ToInt32(txtnumber.Text));
lblmsg.InnerText = word;

}
Praveen Kumar Upadhyay 9-Dec-14 3:37am    
5+ for the lovely program to convert from number to word.
Member 11111143 9-Dec-14 10:09am    
Will this work
Praveen Kumar Upadhyay 9-Dec-14 10:12am    
Why, won't this work? I rated you just because the logic that is been used for converting the number to word.
Member 11111143 9-Dec-14 10:43am    
Actually it is showing me error over ConvertNumbertoWords and over InnerText is there any namespace reqiried for this

hi,

you can call this function on you textbox's ontextchanges event
 
Share this answer
 
 
Share this answer
 
C#
void txtnumber_TextChanged(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(txtnumber.Text))
{
string word = ConvertNumbertoWords(Convert.ToInt32(txtnumber.Text));
lblmsg.InnerText = word;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900