-->

08 February 2020

Number to Word Converter in Asp.Net

  Asp.Net CS By Example       08 February 2020

 Number to Word Converter in Asp.Net 
 In this post,we will seeing how to convert currency to words in asp.net using c# or convert numbers to words in asp.net using c# with example or convert currency / numbers to string in asp.net using c#. To known more about number system Click Here.
 In previous post,we learn convert numbers to Hindi/Marathi words in asp.net using c#, we learn convert numbers to Hindi/Marathi 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: NumberToTextEnglish.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NumberToTextEnglish.aspx.cs" 
Inherits="LearnAsp.Net.NosToWord.NumberToTextEnglish" %>

<!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: NumberToTextEnglish.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 NumberToTextEnglish : System.Web.UI.Page
    {
        String[] Digit19 ={"", "One", "Two", "Three", "four", "Five", "Six", "Seven", 
        "Eight", "Nine", "Ten", "Eleven", "Twelve", 
  "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};

        String[] HigherDigit = { "", "", "Hundred", "Thousand", "Lakh", "Crore", "Arab", "Kharab", "Nil" };
        String[] Map10 = new[] { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

        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 = "ZERO";
            else if (Amt == 100)
                retNos = "HUNDRED";
            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 (Int32.Parse(StrNum.Substring(arrL - (i + 1), step)) < 20)
                            retNos += Digit19[Int32.Parse(StrNum.Substring(arrL - (i + 1), step))] + " " + HigherDigit[HStep] + " ";
                        else if (Int32.Parse(StrNum.Substring(arrL - (i + 1), step)) % 10 == 0)
                            retNos += Map10[Int32.Parse(StrNum.Substring(arrL - (i + 1), step)) / 10] + " ";
                        else
                            retNos += Map10[Int32.Parse(StrNum.Substring(arrL - (i + 1), step)) / 10] + "-" + Digit19[Int32.Parse(StrNum.Substring(arrL - (i + 1), step)) % 10] + " " + HigherDigit[HStep] + " ";

                    }
                    else if (i >= 2)
                    {
                        int b = (arrL == i + 1) && i % 2 == 0 ? 0 : arrL - (i + 1);

                        if (!String.IsNullOrEmpty(Digit19[Int32.Parse(StrNum.Substring(b, 1))]))
                        {
                            String t = StrNum.Substring(b);
                            if (t == "100")
                                retNos += "HUNDRED";
                            else
                                retNos += Digit19[Int32.Parse(StrNum.Substring(b, 1))] + " " + HigherDigit[i] + " ";
                        }
                    }
                    else
                    {
                        int b = arrL <= 1 ? 1 : 2;
                        if (Int32.Parse(StrNum.Substring(arrL - b, b)) < 20)
                            retNos += Digit19[Int32.Parse(StrNum.Substring(arrL - b, b))] + HigherDigit[i] + " ";
                        else if (Int32.Parse(StrNum.Substring(arrL - b, b)) % 10 == 0)
                            retNos += Map10[Int32.Parse(StrNum.Substring(arrL - b, b)) / 10] + " ";
                        else
                            retNos += Map10[Int32.Parse(StrNum.Substring(arrL - b, b)) / 10] + "-" + Digit19[Int32.Parse(StrNum.Substring(arrL - b, b)) % 10] + " ";
                        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 English [full_width]
logoblog

Thanks for reading Number to Word Converter in Asp.Net

Previous
« Prev Post

No comments:

Post a Comment

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