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


NoConverter
Hi

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

7 thoughts on “How to convert Numeric Value to Word formate using C# ?

  1. meganarose February 16, 2013 / 7:19 am

    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.

    • Chandra Dev February 16, 2013 / 11:19 am

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

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

    Nice work,My Dear Friend….

  3. Parvez November 29, 2013 / 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.

    • Chandra Dev November 30, 2013 / 2:32 am

      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.

      • Parvez November 30, 2013 / 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
        Parvez

  4. Parvez November 30, 2013 / 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

    Thanks
    Parvez

Leave a reply to Chandra Dev Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.