Convert Number to Digits , Currency .

Introduction:
This article explain you to convert digits in to words.
Description:
Below C# code is used to convert the number in to words , currency , digit names
To implement this we need to write the code like as shown below

HTML Java Script CSS:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DigitConversion.aspx.cs" 

Inherits="DigitConversion.DigitConversion" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        table {
            border: 1px solid #000;
            border-collapse: collapse;
            margin: auto;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table border="1" cellpadding="5px">
                <tr>
                    <td colspan="3" style="text-align: center;"><b>Convert Digits to Words</b></td>

                </tr>
                <tr>
                    <td>Enter Number</td>
                    <td>:</td>
                    <td>
                        <asp:TextBox ID="TxtNumber" runat="server" Width="95%"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RFVNumber" runat="server" ControlToValidate="TxtNumber" ErrorMessage="Enter Number" Display="Dynamic">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>Number in Words</td>
                    <td>:</td>
                    <td>
                        <asp:Literal ID="LtrNumberInWords" runat="server"></asp:Literal></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td>
                        <asp:Button ID="BtnConvert" runat="server" Text="Word" OnClick="BtnConvert_Click" />
                        <asp:Button ID="BtnWhole" runat="server" Text="Whole Number" OnClick="BtnWhole_Click" />
                        <asp:Button ID="BtnTwoDigitName" runat="server" Text="Digit Names" OnClick="BtnTwoDigitName_Click" />
                    </td>
                </tr>
            </table>

        </div>
    </form>
</body>
</html>


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

namespace DigitConversion
{
    public partial class DigitConversion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnConvert_Click(object sender, EventArgs e)
        {

            DigitsConversion objDigitsConversion = new DigitsConversion();
            LtrNumberInWords.Text = objDigitsConversion.ChangeToWords(TxtNumber.Text, true);
        }

        protected void BtnWhole_Click(object sender, EventArgs e)
        {
            DigitsConversion objDigitsConversion = new DigitsConversion();
            LtrNumberInWords.Text = objDigitsConversion.TranslateWholeNumber(TxtNumber.Text);

        }

       

        protected void BtnTwoDigitName_Click(object sender, EventArgs e)
        {
            DigitsConversion objDigitsConversion = new DigitsConversion();
            LtrNumberInWords.Text = objDigitsConversion.TranslateToDigitName(TxtNumber.Text);

        }

       
    }
}


C# Digit Conversion Class
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DigitConversion
/// </summary>

    public class DigitsConversion
    {
        public DigitsConversion()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public String ToWords(double number, bool IsCurrency)
        {
            String strNumber = number.ToString();
            return ChangeToWords(strNumber, IsCurrency);
        }
        public String ToWords(String number, bool IsCurrency)
        {
            return ChangeToWords(number, IsCurrency);
        }
        public String ChangeToWords(String numb, bool isCurrency)
        {
            String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
            String endStr = (isCurrency) ? ("Only") : ("");
            try
            {
                int decimalPlace = numb.IndexOf(".");
                if (decimalPlace > 0)
                {
                    wholeNo = numb.Substring(0, decimalPlace);
                    points = numb.Substring(decimalPlace + 1);
                    if (Convert.ToInt32(points) > 0)
                    {
                        andStr = (isCurrency) ? ("and") : ("point");// just to separate whole numbers from points/Rupees
                        endStr = (isCurrency) ? ("Rupees " + endStr) : ("");
                        pointStr = TranslateToDigitName(points);
                    }
                }
                val = String.Format("{0} {1}{2} {3}", TranslateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
            }
            catch
            {
                ;
            }
            return val;
        }
        public String TranslateWholeNumber(String number)
        {
            string word = "";
            int numDigits = 0;
            try
            {
                bool beginsZero = false;//tests for 0XX
                bool isDone = false;//test if already translated
                double dblAmt = (Convert.ToDouble(number));
                //if ((dblAmt > 0) && number.StartsWith("0"))

                if (dblAmt > 0)
                {//test for zero or digit zero in a nuemric
                    beginsZero = number.StartsWith("0");

                    //numDigits = number.Length;
                    numDigits = dblAmt.ToString().Length;
                    number = dblAmt.ToString();

                    int pos = 0;//store digit grouping
                    String place = "";//digit grouping name:hundres,thousand,etc...
                    switch (numDigits)
                    {
                        case 1://OneDigitName' range
                            word = OneDigitName(number);
                            isDone = true;
                            break;
                        case 2://TwoDigitName' range
                            word = TwoDigitName(number);
                            isDone = true;
                            break;
                        case 3://hundreds' range
                            pos = (numDigits % 3) + 1;
                            place = " Hundred ";
                            break;
                        case 4://thousands' range
                        case 5:
                            pos = (numDigits % 4) + 1;
                            place = " Thousand ";
                            break;
                        case 6:
                        case 7://millions' range
                            pos = (numDigits % 6) + 1;
                            place = " Lac ";
                            break;
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12://Billions's range
                            pos = (numDigits % 8) + 1;
                            place = " Crore ";
                            break;
                        //add extra case options for anything above Billion...
                        default:
                            isDone = true;
                            break;
                    }
                    if (!isDone)
                    {//if transalation is not done, continue...(Recursion comes in now!!)
                        word = TranslateWholeNumber(number.Substring(0, pos)) + place + TranslateWholeNumber(number.Substring(pos));
                        //check for trailing zeros
                        //if (beginsZero) word = " and " + word.Trim();
                    }
                    //ignore digit grouping names
                    if (word.Trim().Equals(place.Trim())) word = "";
                }
            }
            catch
            {
                ;
            }
            return word.Trim();
        }
        public String TwoDigitName(String digit)
        {
            int digt = Convert.ToInt32(digit);
            String name = null;
            switch (digt)
            {
                case 10:
                    name = "Ten";
                    break;
                case 11:
                    name = "Eleven";
                    break;
                case 12:
                    name = "Twelve";
                    break;
                case 13:
                    name = "Thirteen";
                    break;
                case 14:
                    name = "Fourteen";
                    break;
                case 15:
                    name = "Fifteen";
                    break;
                case 16:
                    name = "Sixteen";
                    break;
                case 17:
                    name = "Seventeen";
                    break;
                case 18:
                    name = "Eighteen";
                    break;
                case 19:
                    name = "Nineteen";
                    break;
                case 20:
                    name = "Twenty";
                    break;
                case 30:
                    name = "Thirty";
                    break;
                case 40:
                    name = "Fourty";
                    break;
                case 50:
                    name = "Fifty";
                    break;
                case 60:
                    name = "Sixty";
                    break;
                case 70:
                    name = "Seventy";
                    break;
                case 80:
                    name = "Eighty";
                    break;
                case 90:
                    name = "Ninety";
                    break;
                default:
                    if (digt > 0)
                    {
                        name = TwoDigitName(digit.Substring(0, 1) + "0") + " " + OneDigitName(digit.Substring(1));
                    }
                    break;
            }
            return name;
        }
        public String OneDigitName(String digit)
        {
            int digt = Convert.ToInt32(digit);
            String name = "";
            switch (digt)
            {
                case 1:
                    name = "One";
                    break;
                case 2:
                    name = "Two";
                    break;
                case 3:
                    name = "Three";
                    break;
                case 4:
                    name = "Four";
                    break;
                case 5:
                    name = "Five";
                    break;
                case 6:
                    name = "Six";
                    break;
                case 7:
                    name = "Seven";
                    break;
                case 8:
                    name = "Eight";
                    break;
                case 9:
                    name = "Nine";
                    break;
            }
            return name;
        }
        public String TranslateToDigitName(String Rupees)
        {
            String cts = "", digit = "", engOne = "";
            for (int i = 0; i < Rupees.Length; i++)
            {
                digit = Rupees[i].ToString();
                if (digit.Equals("0"))
                {
                    engOne = "Zero";
                }
                else
                {
                    engOne = OneDigitName(digit);
                }
                cts += " " + engOne;
            }
            return cts;
        }
    } 

DEMO :

No comments:

Post a Comment

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks