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

About these ads
Posted in C#. 7 Comments »

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

  1. meganarose Says:

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

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

  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

    Thanks
    Parvez


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

Software Engineering

.NET, SQL and Mobile

Darren's Blog

.NET, MVC

Prashant Brall's Blog

Every idea is a great idea as long as it is simple.

Code Wala

Designing and coding

Ambilykk's Blog

My learning bites in Different Technologies

DOT NET RULES

Yes, to dance beneath the diamond sky with one hand waving free

Matt Duffield

One developer to another...

debugmode

debugging Learning

ASP.NET

Just another WordPress.com weblog

Ramani Sandeep's Blog

DotNetting - Fast , Easy Way of Developing Applications

Mehroz's Experiments

My computational experiments, mostly related to .Net and SQL server, to be posted here

Max Vergelli /// dev.blog

Personal code repository of Max Vergelli, web designer and software developer.

Ramblings of the Sleepy...

For an underpaid web architect and overplayed gamer... there is no such thing as sleep.

Code Simplified - Viral Sarvaiya

Code Simplified - Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Sharker Khaleed Mahmud Silverlight Tips & Tricks

Either write something worth reading or do something worth writing

DotNet Strings

.Net Development

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: