Convert Number Into Words Upto 6 Digits

Hello Friends,

Here is the code for Converting a Number Into Words Upto 6 Digits.

Put following code in Default.aspx Page

<%@ 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>Untitled Page</title></head><body>    <form id=”form1″ runat=”server”>    <div>

           Enter any String :

        <asp:TextBox ID=”TextBox1″ runat=”server” Width=”289px” MaxLength=”6″></asp:TextBox>

        <br />

   <asp:Label ID=”Label1″ runat=”server” Width=”558px”></asp:Label>

        <br />

        <asp:Button ID=”Button1″ runat=”server” Height=”28px” onclick=”Button1_Click”

            Text=”Button” Width=”112px” />

      </div>

    </form>

</body>

</html>

 

Put following code in Default.aspx.cs Page

using System;using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts; 

public partial class Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

     string num2string(string no)

    {

        string temp = “”;

        switch (no)

        {

            case “0”: temp = “zero”;

                break;

            case “1”: temp = “one”;

                break;

            case “2”: temp = “two”;

                break;

            case “3”: temp = “three”;

                break;

            case “4”: temp = “four”;

                break;

            case “5”: temp = “five”;

                break;

            case “6”: temp = “six”;

                break;

            case “7”: temp = “seven”;

                break;

            case “8”: temp = “eight”;

                break;

            case “9”: temp = “nine”;

                break;

            case “10”: temp = “ten”;

                break;

            case “11”: temp = “eleven”;

                break;

            case “12”: temp = “twelve”;

                break;

            case “13”: temp = “thirteen”;

                break;

            case “14”: temp = “fourteen”;

                break;

            case “15”: temp = “fifteen”;

                break;

            case “16”: temp = “sixteen”;

                break;

            case “17”: temp = “seventeen”;

                break;

            case “18”: temp = “eighteen”;

                break;

            case “19”: temp = “ninteen”;

                break;

            case “20”: temp = “twenty”;

                break;

            case “30”: temp = “thirty”;

                break;

            case “40”: temp = “fourty”;

                break;

            case “50”: temp = “fifty”;

                break;

            case “60”: temp = “sixty”;

                break;

            case “70”: temp = “seventy”;

                break;

            case “80”: temp = “eighty”;

                break;

            case “90”: temp = “ninty”;

                break;

        }

        return temp;

    }

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        if ((TextBox1.Text!=null)&&(TextBox1.Text!=””))

        {

 

            string str = int.Parse(TextBox1.Text).ToString();

            string target = “”;

 

            for (int i = 0; i < str.Length; i++)

            {

                if (str.Length – i == 1)

                {

                    target += ” ” + num2string(str[i].ToString());

                }

                else if (str.Length – i == 2)

                {

                    if (str[i].ToString() == “1”)

                    {

                        target += ” ” + num2string(str[i].ToString() + str[i + 1].ToString());

                        i++;

                    }

                    else

                    {

                        target += ” ” + num2string(str[i].ToString() + “0”);

                    }

                }

                else if (str.Length – i == 3)

                {

                    target += ” ” + num2string(str[i].ToString());

                    target += ” ” + “Hundred”;

                }

                else if (str.Length – i == 4)

                {

                    target += ” ” + num2string(str[i].ToString());

                    target += ” ” + “Thousand”;

                }

                else if (str.Length – i == 5)

                {

                    if (str[i].ToString() == “1”)

                    {

                        target += ” ” + num2string(str[i].ToString() + str[i + 1].ToString());

                        i++;

                        target += ” ” + “Thousand”;

                    }

                    else

                    {

                        target += ” ” + num2string(str[i].ToString() + “0”);

                    }

                }

                else if (str.Length – i == 6)

                {

                    target += ” ” + num2string(str[i].ToString());

                    target += ” ” + “Lakh”;

                }

 

            }

            Label1.Text = target;

        }

    }

}

 

Download Demo

Thanks …

Advertisements

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