Skip to main content

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.
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 static string NumbersToWords(int inputNumber)
{
    int inputNo = inputNumber;

    if (inputNo == 0)
        return "Zero";

    int[] numbers = new int[4];
    int first = 0;
    int u, h, t;
    System.Text.StringBuilder sb = new System.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]);
    }
    return sb.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. Thanks a lot for all your efforts.

    saved my lots of time....

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice code.However prints "and" at the beggining for numbers < 100.eg: if input number is 25 output is "and Twenty Five" instead of "Twenty Five"

    ReplyDelete
  4. If you like our articles please subscribe to our newsletter. Thanks

    ReplyDelete
  5. Nice Articles and very good content you posted, keep going to post new code, i'll come regullaryly

    http://interview-question-answer.weebly.com/cnet/c-interview-questions-and-answers

    ReplyDelete
  6. Good work awesome coding..... Only change to this part is needed : if (h > 0 && i == 0) sb.Append("and "); because above coding shows o/p as : "and two". Really helped me lot and saved my time.

    ReplyDelete
  7. Very Nice program. Saved lot of time.

    To get the proper message for numbers below 100, just replace last line
    return sb.ToString().TrimEnd();
    by
    string sn = string.Empty;
    string s = sb.ToString().TrimEnd();
    if (s.IndexOf("and") == 0) sn = s.Remove(0, 4);
    else sn = s;
    return sn;

    Anyways thanks a lot.

    ReplyDelete
  8. Great..working...thanks

    ReplyDelete
  9. How to convert numbers to word like 12345 as one two three four five

    ReplyDelete
  10. please help me i'm getting problem to convert like 12345 as one two three four five in c# or javascript

    ReplyDelete
  11. If anybody have the solution then please send me at"chandansingh913@gmail.com"

    Thanks in Advance!!!!

    ReplyDelete

Post a Comment

Popular posts from this blog

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.