We used to get scenario to convert Numeric value to word format so many time in our project. We can do this task like this
Step1: Create on static class like this
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Converter /// </summary> public class Converter { public static string NumberToWords(int number) { if (number == 0) return "zero"; if (number < 0) return "minus " + NumberToWords(Math.Abs(number)); string words = ""; if ((number / 1000000) > 0) { words += NumberToWords(number / 1000000) + " million "; number %= 1000000; } if ((number / 1000) > 0) { words += NumberToWords(number / 1000) + " thousand "; number %= 1000; } if ((number / 100) > 0) { words += NumberToWords(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; } }
Step 2: Create the aspx code like this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtNo" runat="server" /> <br /> <asp:Button ID="btnSubit" runat="server" Text="Submit" onclick="btnSubit_Click" /> <br /> <asp:Label ID="lblmsg" ForeColor="Red" runat="server" /> <br /> </div> </form> </body> </html>
Steps 3: Call the static class in Code behind file like this
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubit_Click(object sender, EventArgs e) { int inputVal=Convert.ToInt32(txtNo.Text); lblmsg.Text = Converter.NumberToWords(inputVal); } }
Hi,
When I tried to implement this code I get an compiler error saying “ASP.numeric_aspx.GetTypeHashCode()’: no suitable method found to override”. What may be the cause for this error.
Sorry, I forget to post code behind file of C#. Please check it now.
Nice work,My Dear Friend….
Nice work
Please work on this too
if we click with a blank text box the following error displays
Server Error in ‘/login_page’ Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Hi,
Thanks for posting comment. If you will validation of Blank TextBox using Asp.net Validation control then you will not get that errors.
If you have still problem, let me explain the scenario of problem.
I have used validation my friend.
if user types varchar in textbox by mistake it should through an error
can u please work on it and let me know
Thanks in Advance
Parvez
Anyways i used Required field validation control
and set an error message “please enter valid Number”
the error displays if i left blank and submit
let me know if i enter varchar it should show the same error
Thanks
Parvez