Home > C# > How to convert Numeric Value to Word formate using C# ?

How to convert Numeric Value to Word formate using C# ?


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];
            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">
    <form id="form1" runat="server">
 <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 />

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);

About these ads
Categories: C#
  1. February 16, 2013 at 7:19 am

    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.

    • February 16, 2013 at 11:19 am

      Sorry, I forget to post code behind file of C#. Please check it now.

  2. Sumith
    November 21, 2013 at 12:02 pm

    Nice work,My Dear Friend….

  3. Parvez
    November 29, 2013 at 5:35 am

    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.

    • November 30, 2013 at 2:32 am

      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.

      • Parvez
        November 30, 2013 at 5:15 pm

        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

  4. Parvez
    November 30, 2013 at 5:18 pm

    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


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 50 other followers

%d bloggers like this: