Monday, July 1, 2013

Price/Number convert into words in C# Asp .Net

==>> In this article I am posting how to convert Price/Number in words using c# Asp .Net. Some time we need convert Price/Number in words in banking /financial type application and in invoice report we need this so this post will heplfull for you.
 For this you need take a new website and add a textbox,label and button then add the  cs code given below : -

aspx Page Code:-


TextBox

<asp:TextBox ID="TxtPrice" runat="server" MaxLength="9"></asp:TextBox>

Button

<asp:Button ID="BtnConvert" runat="server" Text="Convert In Words"  ValidationGroup="val"
             onclick="BtnConvert_Click" />

Label

<asp:Label ID="LblPriceinWords" runat="server" BackColor="#ccffff" ></asp:Label>




 aspx.cs Page Code:-




 public static string AmountToWord(int number)
    {
        if (number == 0) return "Zero";

        if (number == -2147483648)
       return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";

        int[] num = new int[4];
        int first = 0;
        int u, h, t;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (number < 0)
        {
            sb.Append("Minus ");
            number = -number;
        }

        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 " };

        num[0] = number % 1000; // units
        num[1] = number / 1000;
        num[2] = number / 100000;
        num[1] = num[1] - 100 * num[2]; // thousands
        num[3] = number / 10000000; // crores
        num[2] = num[2] - 100 * num[3]; // lakhs

        //You can increase as per your need.

        for (int i = 3; i > 0; i--)
        {
            if (num[i] != 0)
            {
                first = i;
                break;
            }
        }


        for (int i = first; i >= 0; i--)
        {
            if (num[i] == 0) continue;

            u = num[i] % 10; // ones
            t = num[i] / 10;
            h = num[i] / 100; // hundreds
            t = t - 10 * h; // tens

            if (h > 0) sb.Append(words0[h] + "Hundred ");

            if (u > 0 || t > 0)
            {
                if (h == 0)
                    sb.Append("");
                else
                    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() + " Rupees only";
    }


    protected void BtnConvert_Click(object sender, EventArgs e)
    {
        int price = Convert.ToInt32(TxtPrice.Text);
        LblPriceinWords.Text = AmountToWord(price);
    }

Example:-





Hpoe this will help you........


Download Sample Code :-


No comments:

Post a Comment