-->

10 January 2020

Converting Numbers to Words using the Indian Numbering System In C#

  Asp.Net CS By Example       10 January 2020

 Number to Word Converter in Asp.Net 

 In this post , I present a simple function for converting numbers to words using the Indian numbering system. The program was restricted to integers in the range of an Int32 which I believe is the most useful case. In below in have given function which convert number to word which in written in c#.net.

 In previous post,we learn convert numbers to English words in asp.net using c#, we learn convert numbers to Hindi/Devnagari words in Crystal Report,How to connect to Database using ado.net, Gridview Control, Web.Config file, Application State, Asp.Net Page Life Cycle. Now we will see how to convert currency / numbers to words / string in asp.net using c# by example.

  Code: NumberToWord_Hindi.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NumberToWord_Hindi.aspx.cs" 
Inherits="LearnAsp.Net.NosToWord.NumberToWord_Hindi" %>

<!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>
    <h3>Converting Numbers to Words In C#</h3>
        <asp:TextBox ID="txtNumber" runat="server" MaxLength="30"></asp:TextBox>  
        <asp:Button ID="btnConvert" runat="server" Text="Convert" 
            onclick="btnConvert_Click" />
            <br />
        <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
    

  Code: NumberToWord_Hindi.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LearnAsp.Net.NosToWord
{
    public partial class NumberToWord_Hindi : System.Web.UI.Page
    {
        String[] HundredMarathiDigit ={"", "एक", "दोन", "तीन", "चार", "पाच", "सहा", "सात", "आठ", "नऊ", "दहा", 
            "अकरा","बारा","तेरा","चौदा","पंधरा","सोळा","सतरा","अठरा", "एकोणवीस","वीस",
            "एकवीस","बावीस","तेवीस","चोवीस","पंचवीस","सव्वीस","सत्तावीस","अठ्ठावीस","एकोणतीस","तीस",
            "एकतीस","बत्तीस","तेहेतीस","चौतीस","पस्तीस","छत्तीस","सदतीस","अडतीस","एकोणचाळीस","चाळीस",
            "एक्केचाळीस","बेचाळीस","त्रेचाळीस","चव्वेचाळीस","पंचेचाळीस","सेहेचाळीस","सत्तेचाळीस","अठ्ठेचाळीस","एकोणपन्नास","पन्नास",
            "एक्कावन्न","बावन्न","त्रेपन्न","चोपन्न","पंचावन्न",
                    "छप्पन्न","सत्तावन्न","अठ्ठावन्न","एकोणसाठ","साठ",
            "एकसष्ठ","बासष्ठ","त्रेसष्ठ","चौसष्ठ","पासष्ठ","सहासष्ठ","सदुसष्ठ","अडुसष्ठ","एकोणसत्तर","सत्तर",
            "एक्काहत्तर","बाहत्तर","त्र्याहत्तर","चौर्याहत्तर","पंच्याहत्तर","शहात्तर","सत्याहत्तर","अठ्ठ्याहत्तर","एकोण ऐंशी","ऐंशी",
            "एक्क्य़ाऐंशी","ब्याऐंशी","ञ्याऐंशी","चौऱ्याऐंशी","पंच्याऐंशी","शहाऐंशी","सत्त्याऐंशी","अठ्ठ्याऐंशी","एकोणनव्वद","नव्वद",
            "एक्क्याण्णव","ब्याण्णव","त्र्याण्णव","चौऱ्याण्णव","
                    पंच्याण्णव","शहाण्णव","सत्त्याण्णव","अठ्ठ्याण्णव","नव्व्याण्णव","शंभर"};

        String[] HigherDigitMarathiNumber = { "", "", "शे", "हजार", "लाख", "करोड़", "अरब", "खरब", "नील" };
        
        String[] ArrIdx = { "4", "6", "8", "10", "12", "14", "16", "18" };

        String NumberToWord( Decimal Amt)
        {
             String retNos = "";
             String Sign = "";
            if(Amt<0)
                Sign = " - ";

            Amt = Math.Abs(Amt);
            int arrL = (Amt.ToString()).Length;
            String StrNum = Amt.ToString();
            if (Amt == 0)
                retNos = "शून्य";
            else if (Amt == 100)
                retNos = "शंभर";
            else
                for (int i = arrL - 1; i >= 0; i--)
                {
                    int step = 1;
                    int HStep = 2;
                    Boolean is2Digit = false;
                    if(ArrIdx.Contains(i.ToString()))
                    {
                        is2Digit = true;
                        step = 2;
                    }
                    int a = i % 2 == 1 ? 1 : 0;

                    HStep = 2 + ((i - 3) / 2) + ((i - 3) % 2) + a;
                    if (i >= 3)
                    {
                        if (!String.IsNullOrEmpty(HundredMarathiDigit[Int32.Parse(StrNum.Substring(arrL - (i + 1), step))]))
                            retNos += HundredMarathiDigit[Int32.Parse(StrNum.Substring(arrL - (i + 1), step))] + HigherDigitMarathiNumber[HStep] + " ";
                    }
                    else if (i >= 2)
                    {
                       int b = (arrL == i + 1) && i % 2 == 0 ? 0 : arrL - (i + 1);

                        if (!String.IsNullOrEmpty(HundredMarathiDigit[Int32.Parse(StrNum.Substring(b, 1))]))
                        {
                            String t = StrNum.Substring(b);
                            if (t == "100")
                                retNos += "शंभर";
                            else
                                retNos += HundredMarathiDigit[Int32.Parse(StrNum.Substring(b, 1))] + HigherDigitMarathiNumber[i] + " ";
                        }
                    }
                    else
                    {
                        int b = arrL <= 1 ? 1 : 2;
                        retNos += HundredMarathiDigit[Int32.Parse(StrNum.Substring(arrL - b, b))] + HigherDigitMarathiNumber[i] + " ";
                        break;
                    }

                    if (is2Digit)
                        i--;
                }
            return Sign+retNos;
        }

        protected void btnConvert_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtNumber.Text))
            {
                lblResult.Text = "Plz Enter Nos in text box...";
                txtNumber.Focus();
                return;
            }
            Decimal iNo = Convert.ToDecimal(txtNumber.Text);
            String str = NumberToWord(iNo);

            lblResult.Text =  str;
        }
    }
}

  Output:
Number To Word In Hindi
[full_width]
logoblog

Thanks for reading Converting Numbers to Words using the Indian Numbering System In C#

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.