Code To Convert rupees(numbers) into words using C#.Net

Introduction:
In my previous article I have explained about how to validate emailid using javascript. In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net.

If you are looking for Convert numbers to words (123 into one two three) in c#.net. then check this link Convert numbers to words (123 into one two three) in c#.net

Explanation:
For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label.
Below is the C# code used to do this functionality.
public staticstring NumbersToWords(intinputNumber)
{
    intinputNo = inputNumber;
    if(inputNo == 0)
        return“Zero”;
    int[] numbers = new int[4];
    intfirst = 0;
    intu, h, t;
    System.Text.StringBuilder sb = newSystem.Text.StringBuilder();
    if(inputNo < 0)
    {
        sb.Append(“Minus “);
        inputNo = -inputNo;
    }
    string[] words0 = {“” ,“One “, “Two “, “Three “, “Four “,
            “Five “ ,“Six “, “Seven “, “Eight “, “Nine “};
    string[] words1 = {“Ten “, “Eleven “, “Twelve “, “Thirteen “, “Fourteen “,
            “Fifteen “,“Sixteen “,“Seventeen “,“Eighteen “, “Nineteen “};
    string[] words2 = {“Twenty “, “Thirty “, “Forty “, “Fifty “, “Sixty “,
            “Seventy “,“Eighty “, “Ninety “};
    string[] words3 = { “Thousand “, “Lakh “, “Crore “ };
    numbers[0] = inputNo % 1000; // units
    numbers[1] = inputNo / 1000;
    numbers[2] = inputNo / 100000;
    numbers[1] = numbers[1] – 100 * numbers[2]; // thousands
    numbers[3] = inputNo / 10000000; // crores
    numbers[2] = numbers[2] – 100 * numbers[3]; // lakhs
    for(int i = 3; i > 0; i–)
    {
        if(numbers[i] != 0)
        {
            first = i;
            break;
        }
    }
    for(int i = first; i >= 0; i–)
    {
        if(numbers[i] == 0) continue;
        u = numbers[i] % 10; // ones
        t = numbers[i] / 10;
        h = numbers[i] / 100; // hundreds
        t = t – 10 * h; // tens
        if(h > 0) sb.Append(words0[h] + “Hundred “);
        if(u > 0 || t > 0)
        {
            if (h > 0 || i == 0) sb.Append(“and “);
            if (t == 0)
                sb.Append(words0[u]);
            else if (t == 1)
                sb.Append(words1[u]);
            else
                sb.Append(words2[t – 2] + words0[u]);
        }
        if(i != 0) sb.Append(words3[i – 1]);
    }
    returnsb.ToString().TrimEnd();
}



Below is the aspx code.
<form id=”form1″ runat=”server”>
<div>
    <table style=”width: 50%;”>
        <tr>
            <td>
                Input Number
            </td>
            <td>
                <asp:TextBox ID=”txtNumber”runat=”server”></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID=”btnConvert”Text=”Convert To Words” runat=”server”OnClick=”btnConvert_Click”/>
            </td>
        </tr>
        <tr>
            <td colspan=”2″>
                <asp:Label ID=”lblWords”runat=”server”></asp:Label>
            </td>
        </tr>
    </table>
</div>
</form>
Do you like this article? Help us to improve. Please post your comments below.


Download Source Code:

Code Convert rupees(numbers) into words using C#.Net

Comments

  1. By Anonymous

  2. By Anonymous

  3. By Anonymous

  4. By sarjan Desai

  5. By Anonymous

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!