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" "">

<html xmlns="">
<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);

Posted in C#. 7 Comments »

7 Responses to “How to convert Numeric Value to Word formate using C# ?”

  1. meganarose Says:

    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.

    • Chandra Dev Says:

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

  2. Sumith Says:

    Nice work,My Dear Friend….

  3. Parvez Says:

    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.

    • Chandra Dev Says:

      Thanks for posting comment. If you will validation of Blank TextBox using Validation control then you will not get that errors.

      If you have still problem, let me explain the scenario of problem.

      • Parvez Says:

        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 Says:

    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


Leave a Reply

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

You are commenting using your 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

%d bloggers like this: