ASP.NET : Converting Numbers to Words

In this article, I'm trying to explain how to convert given numbers into words using c# programming language in asp.net. This is the requirement that I implemented in one of my previous projects, so I thought this concept was very useful for those who started they carried as a .net developer in the beginning stage.

A preview of this project I already showed in the form of an image, you can find below.

HTML Markup: Design Page

Let's design our user interface according to our requirements which I showed in the above image.

Try to implement it in your way, if you not then you can copy and paste this below code.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Numbertowords.aspx.cs" Inherits="ASPNET_Numbertowords" %>
<!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>
        <b>Enter Amount : </b></b><asp:TextBox ID="txt_number" runat="server" Width="311px"></asp:TextBox>&nbsp;
        <asp:Button ID="Button1"
            runat="server" Text=" InWords " onclick="Button1_Click" />
    </div>
    <asp:Label ID="lblresult" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>

C# Coding: Numbers to Words Logic

In this code behind the page, I create a method called Inwords it is a private static method and this method requires input as an integer value and the return type is a string.

This method contains logic which is used to convert a given integer number into a string and it returns that string as output to the button click event where it is shown on the page as output.

Button1_Click Event fires whenever the end user clicks on this button it takes the textbox value i.e txt_number and passes that value into Inwords method.

By default textbox value is a string so we need to parse that string value into an integer because the Inwords method accepts only integer values and it returns as a string.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ASPNET_Numbertowords : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        string converttowords = Inwords(Convert.ToInt32(txt_number.Text.Trim()));
        lblresult.Text = "Amount InWords : " + converttowords + " Rupees Only.";  
    }
    private static string Inwords(int n)
    {
        if (n == 0)
            return "ZERO";
        if (n < 0)
            return "minus " + Inwords(Math.Abs(n));
        string inwords = "";

        if ((n / 10000000) > 0)
        {
            inwords += Inwords(n / 10000000) + " CRORE ";
            n = n % 10000000;
        }

        if ((n / 100000) > 0)
        {
            inwords += Inwords(n / 100000) + " LAKH ";
            n = n % 100000;
        }
        if ((n / 1000) > 0)
        {
            inwords += Inwords(n / 1000) + " THOUSAND ";
            n = n % 1000;
        }
        if ((n / 100) > 0 || (n / 1000) < 0)
        {
            inwords += Inwords(n / 100) + " HUNDRED ";
            n = n % 100;
        }
        if (n > 0)
        {
            if (inwords != "")
                inwords += "AND ";
            var units = new[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" };
            var tens = new[] { "ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" };

            if (n < 20)
                inwords += units[n];
            else
            {
                inwords += tens[n / 10];
                if ((n % 10) > 0)
                    inwords += " " + units[n % 10];
            }
        }
        return inwords;
    }
}

Post a Comment

0 Comments