Pages

Ruchi Tech

Monday, 17 September 2012

How to Convert a numbers to words in asp.net

This article explains about how to convert a numeric value to words.

For Example: Numeric: 500
                       In Words: Five hundred Only


Write a Code:

staticclass NumberToWord
{ 
  private static string[] _ones =
   {
     "zero",
     "one",
     "two",
     "three",
     "four",
     "five",
     "six",
     "seven",
     "eight",
     "nine"
   }; 
 private static string[] _teens =
  { 
     "ten",
     "eleven",
     "twelve",
     "thirteen",
     "fourteen",
     "fifteen",
     "sixteen",
     "seventeen",
     "eighteen",
     "nineteen"
  };
 private static string[] _tens =
  {
     "",
     "ten",
     "twenty",
     "thirty",
     "forty",
     "fifty",
     "sixty",
     "seventy",
     "eighty",
     "ninety"
  };
 private static string[] _thousands =
  {
     "",
     "thousand",
     "million",
     "billion",
     "trillion",
     "quadrillion"
  };

 public static string Convert(decimal value)
  { 

    string digits, temp;
    bool showThousands = false;
    bool allZeros = true;           
    StringBuilder builder = new StringBuilder();
    digits = ((long)value).ToString(); 

    for(int i = digits.Length-1; i >= 0; i--)
    {

      int ndigit = (int)(digits[i] - '0');
      int column = (digits.Length - (i + 1));
      switch (column % 3)
      { 

  case 0:   
      
showThousands = true; 

       if (i == 0)
        
{
           temp =
String.Format("{0} ", _ones[ndigit]);
        
}
      
else if (digits[i - 1] == '1')
        
{
           temp =
String.Format("{0} ", _teens[ndigit]);
          
i--;
         }
      
else if (ndigit != 0)
       
{
          temp =
String.Format("{0} ", _ones[ndigit]);
       
}
      
else
       
{
          temp =
String.Empty;
         
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
         
showThousands = true;
         
else
         
showThousands = false;
       
}
      
if (showThousands)
      
{
       
if (column > 0)
       
{
         temp =
String.Format("{0}{1}{2}",
               
temp,_thousands[column / 3], allZeros ? " " : " ");
        }

        allZeros =
false;
      
}
       builder.Insert(0, temp);
      
break;

 
case 1:
        if (ndigit > 0)
        {
         temp =
String.Format("{0}{1}",_tens[ndigit],

         (digits[i + 1] != '0') ? "-" : " ");builder.Insert(0, temp);
       }
      
break;
 

case 2:
      if (ndigit > 0)
        {
         temp =
String.Format("{0} hundred ", _ones[ndigit]);
                    builder.Insert(0, temp);
       }
      
break;
     }}
   builder.AppendFormat(
"only", (value - (long)value) * 100);
   return String.Format("{0}{1}",
   Char.ToUpper(builder[0]),
   builder.ToString(1, builder.Length - 1));
  }
 

}

 

Add following code on Button_Click event

decimal number;
if (!string.IsNullOrEmpty(txtAmount.Text) && decimal.TryParse(txtAmount.Text.Trim(), out number))
{
   lblAmountChar.Text = "(" + NumberToWord.Convert(number) + ")";
}

It convert given number to words. Thanks.

13 comments:

  1. This is really very interesting about converting numbers to words.

    Convert ASP to ASP.Net

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

    ReplyDelete
  3. Keep posting articles like this one , good job ,you may be interested in this article: gmail.comma.

    ReplyDelete