Tuesday, 22 May 2012

Numeric to Words(ASP.NET C#)


Numeric to Words in ASP.NET  C#



------------------------------------------------



call the function
----------------------------

      lblamtword.Text = NumberToCurrencyText(Convert.ToDecimal(ObjPrintChequeBL.CashAmt), System.MidpointRounding.ToEven);
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Public static string NumberToCurrencyText(decimal number, MidpointRounding midpointRounding)
    {

        // Round the value just in case the decimal value is longer than two digits

        number = decimal.Round(number, 2, midpointRounding);

        string wordNumber = string.Empty;

        // Divide the number into the whole and fractional part strings

        string[] arrNumber = number.ToString().Split('.');

        // Get the whole number text

        long wholePart = long.Parse(arrNumber[0]);

        string strWholePart = NumberToText( wholePart);

        // For amounts of zero dollars show 'No Dollars...' instead of 'Zero Dollars...'

        wordNumber = (wholePart == 0 ? "No" : strWholePart) + (wholePart == 1 ? " Ruppe and " : " Ruppes and ");

        // If the array has more than one element then there is a fractional part otherwise there isn't

        // just add 'No Cents' to the end

        if (arrNumber.Length > 1)
        {

            // If the length of the fractional element is only 1, add a 0 so that the text returned isn't,

            // 'One', 'Two', etc but 'Ten', 'Twenty', etc.

            long fractionPart = long.Parse((arrNumber[1].Length == 1 ? arrNumber[1] + "0" : arrNumber[1]));

            string strFarctionPart = NumberToText(fractionPart);

            wordNumber += (fractionPart == 0 ? " No" : strFarctionPart) + (fractionPart == 1 ? " Paisa" : " Paisa");

        }

        else

            wordNumber += "No Paisa";

        return wordNumber;

    }


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


     public static  string NumberToText(long    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";
         long [] num = new long [4];
         int first = 0;
         long  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
         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 || 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();
     }

No comments:

Post a Comment