Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Saturday, 12 March 2016

How to Convert Number to Word in Asp.Net

how to convert numbers to words in asp.net using c# with example.

To convert numbers to words in asp.net using c# we need to write the code like as shown below


Create a ASPX page and name it convert-number-to-words.aspx, in the page we take a TextBox, a Label and two Button control and name it to txtNumber, lblNetAmountUSDInWords, btnSubmit and btnCancel.



convert-number-to-words.aspx


    <form id="form1" runat="server">

     <table width="600" height="80%" align="center" style="border: 1px solid #000">
      <tr>
        <td width="30%"  class="clsTDTextField">
         <span style="font-family: Calibri; font-size: 18px; font-weight: bold;">
            Enter Number:
          </span>
         </td>
         <td width="50%" align="left" valign="top">
          <span style="font-family: Calibri; font-size: 18px; font-weight: bold;">
            <asp:TextBox ID="txtNumber" class="textbox" runat="server"></asp:TextBox>
          </span>
                  
         </td>
       </tr>
       <tr>
         <td width="30%">
               
          </td>
          <td width="50%" align="left" valign="top">
           <span style="font-family: Calibri; font-size: 18px; font-weight: bold;">
             <asp:Label ID="lblNetAmountUSDInWords" runat="server"></asp:Label>
           </span>
          </td>
        </tr>
        <tr>
         <td colspan="2">
          <br />
         </td>
        </tr>
        <tr>
          <td width="30%">
          </td>
          <td>
           <asp:Button ID="btnSubmit" runat="server"
            Text="Show in Words" OnClick="btnSubmit_Click" />
            &nbsp;
          <asp:Button ID="btnCancel" runat="server" CausesValidation="false"
           Text="Cancel" OnClick="btnCancel_Click" />
          </td>
         </tr>
        </table>
    </form>

convert-number-to-words.aspx.cs

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

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int NetAmountUSD = Convert.ToInt32(Math.Round(Convert.ToDecimal(txtNumber.Text)));
       
        //Translate Amount In Number To Word(that is net pay amount or final paybale amount to employee)
        string word = ConvertNumbertoWords(NetAmountUSD);
        lblNetAmountUSDInWords.Text = word;
    }
    #region numberic value to word
    public static string ConvertNumbertoWords(int number)
    {
        if (number == 0)
            return "ZERO";
        if (number < 0)
            return "minus " + ConvertNumbertoWords(Math.Abs(number));
        string words = "";
        if ((number / 1000000) > 0)
        {
            words += ConvertNumbertoWords(number / 1000000) + " MILLION ";
            number %= 1000000;
        }
        if ((number / 1000) > 0)
        {
            words += ConvertNumbertoWords(number / 1000) + " THOUSAND ";
            number %= 1000;
        }
        if ((number / 100) > 0)
        {
            words += ConvertNumbertoWords(number / 100) + " HUNDRED ";
            number %= 100;
        }
        if (number > 0)
        {
            if (words != "")
                words += "AND ";
            var unitsMap = new[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" };
            var tensMap = new[] { "ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += " " + unitsMap[number % 10];
            }
        }
        return words;
    }
    #endregion
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtNumber.Text = "";
        lblNetAmountUSDInWords.Text = "";
    }
}
 
Result:


1. We enter 4560 number in the textbox, it display the result in words

FOUR THOUSAND FIVE HUNDRED AND SIXTY




2. We enter 4560.32 numbers in the textbox, the entered decimal value less than 0.50 than its display the result in words with truncated decimal value.

FOUR THOUSAND FIVE HUNDRED AND SIXTY



3. We enter 4560.75 numbers in the textbox, the entered decimal value greater than 0.50 than its display the result in words with value increments by 1.

FOUR THOUSAND FIVE HUNDRED AND SIXTY ONE