How to convert a Number to words

No.of Views1872
Bookmarked0 times
Downloads 
Votes0
By  Kirti.M.Darji   On  13 Oct 2011 12:10:03
Tag : ASP.NET , General
This article I am going to discuss function which convert numeric value to word for that we create one web page which return convert number to word.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

This article I am going to discuss function which convert numeric value to word  for that we create one web page which return convert number to word.
To achieve this I  create one simple webpage which have one textbox and one button when click on button number is converted to text and display in label see below image.

Image Loading

I have create on class which convert given number to text

static class 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"
};
 
// US Nnumbering:
private static string[] _thousands =
{
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
};
 
/// <summary>
/// Converts a numeric value to words suitable for the portion of
/// a check that writes out the amount.
/// </summary>
/// <param name="value">Value to be converted
/// <returns></returns>
public static string Convert(decimal value)
{
string digits, temp;
bool showThousands = false;
bool allZeros = true;
StringBuilder builder = new StringBuilder();
// Convert integer portion of value to string
digits = ((long)value).ToString();
// Traverse characters in reverse order
for (int i = digits.Length - 1; i >= 0; i--)
{
int ndigit = (int)(digits[i] - '0');
int column = (digits.Length - (i + 1));
 
// Determine if ones, tens, or hundreds column
switch (column % 3)
{
case 0:        // Ones position
showThousands = true;
if (i == 0)
{
// First digit in number (last in loop)
temp = String.Format("{0} ", _ones[ndigit]);
}
else if (digits[i - 1] == '1')
{
// This digit is part of "teen" value
temp = String.Format("{0} ", _teens[ndigit]);
// Skip tens position
i--;
}
else if (ndigit != 0)
{
// Any non-zero digit
temp = String.Format("{0} ", _ones[ndigit]);
}
else
{
// This digit is zero. If digit in tens and hundreds
// column are also zero, don't show "thousands"
temp = String.Empty;
// Test for non-zero digit in this grouping
if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
showThousands = true;
else
showThousands = false;
}
 
// Show "thousands" if non-zero in grouping
if (showThousands)
{
if (column > 0)
{
temp = String.Format("{0}{1}{2}",
temp,
_thousands[column / 3],
//allZeros ? " " : ", ");
allZeros ? " " : " ");
}
// Indicate non-zero digit encountered
allZeros = false;
}
builder.Insert(0, temp);
break;
 
case 1:        // Tens column
if (ndigit > 0)
{
temp = String.Format("{0}{1}",
_tens[ndigit],
(digits[i + 1] != '0') ? "-" : " ");
builder.Insert(0, temp);
}
break;
 
case 2:        // Hundreds column
if (ndigit > 0)
{
temp = String.Format("{0} hundred ", _ones[ndigit]);
builder.Insert(0, temp);
}
break;
}
}
 
// Append fractional portion/cents
builder.AppendFormat(" DOLLARS and {0:00} / 100", (value - (long)value) * 100);
 
// Capitalize first letter
return String.Format("{0}{1}",
Char.ToUpper(builder[0]),
builder.ToString(1, builder.Length - 1));
}
}

I made following code on button click event

protected void btnconvert_Click(object sender, EventArgs e)
{
decimal number;
if(!string.IsNullOrEmpty(txtnumber.Text)&& decimal.TryParse(txtnumber.Text.Trim(),out number))
{
lblText.Text=NumberToWord.Convert(number);
}
 
}

it covert given number to word. This may be useful for you. Stay turned for more.
Thanks for reading. Happy coding!!

 

 

 
Sign Up to vote for this article
 
About Author
 
Kirti.M.Darji
Occupation-Software Engineer
Company-Maven-Infosoft Pvt.Ltd
Member Type-Expert
Location-Not Provided
Joined date-03 Jun 2009
Home Page-
Blog Page-http://kirtimdarji.blogspot.com/
I am Kirti M. Darji Senior Software Developer Having 3.5 Years Experience In Microsoft .Net(c#) Technologies.
 
 
Other popularSectionarticles
    Google has launched Ajax API for different purposes video search is one of them. Many user's gets confused all about it many times how to get google key, how to embed video search in web application. So here I am explaining how to use video search in web application.
    Published Date : 22/Aug/2010
    In visual studio 2008 nested master page concept is introduced, to make page template
    Published Date : 16/Feb/2010
    The problem arises when you install IIS after installing ASP.NET. If you do this, IIS will configure itself for the ASP.NET version that ships with your Windows edition that might be an older version (e.g. version 2.0) and you won’t be able to run any web application built using a later version of ASP.NET.
    Published Date : 21/Jan/2011
    This is just a helper article which may help you to decide which technology you might want to go.
    Published Date : 20/Aug/2010
    Set Start Up page on Visual studio IDE
    Published Date : 02/Aug/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</