Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net Convert Numbers to Words (String) in C#

Aug 1, 2014
Introduction:

Here I will explain how to convert numbers to words in
asp.net using c# with example or convert numbers to string in asp.net using c#.

Description:

In previous post I explained jQuery tag cloud example in asp.net,
sitemap control example, send html page as mail body in asp.net, send mail with images using gmail user credentials, send multiple attachments with email in asp.net, how to send mail with attachment in asp.net and many more articles related to asp.net using c#, vb.net. Now I will explain how to convert numbers to words in asp.net using c# with example.

To convert numbers to words in asp.net using c# we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>C# - Convert Number to Words in Asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Value: <asp:TextBox ID="txtnumber" runat="server" /><br />
<asp:Button ID="btnClick" runat="server" Text="Convert" onclick="btnClick_Click" /><br />
<label id="lblmsg" runat="server" />
</div>
</form>
</body>
</html>
After completion of aspx page write the following code in codebehind

C# Code


using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick_Click(object sender, EventArgs e)
{
string word = ConvertNumbertoWords(Convert.ToInt32(txtnumber.Text));
lblmsg.InnerText = word;
}
public static string ConvertNumbertoWords(int number)
{
if (number == 0)
return "ZERO";
if (number < 0)
return "minus " + ConvertNumbertoWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += ConvertNumbertoWords(number / 1000000) + " MILLION ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += ConvertNumbertoWords(number / 1000) + " THOUSAND ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += ConvertNumbertoWords(number / 100) + " HUNDRED ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "AND ";
var unitsMap = new[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" };
var tensMap = new[] { "ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" };

if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += " " + unitsMap[number % 10];
}
}
return words;
}
}
Demo



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Vinay Arora said...

Try to enter
589652
The result will be "FIVE HUNDRED AND EIGHTY NINE THOUSAND SIX HUNDRED AND FIFTY TWO";

The correct result should be "FIVE MILLION EIGHTY NINE THOUSAND SIX HUNDRED AND FIFTY TWO".

Solution: Divide the number with 100000 ( 1 Lakh) instead of 1000000 (1 Million)
if ((number / 100000) > 0)
{
words += ConvertNumbertoWords(number / 100000) + " MILLION ";
number %= 100000;
}

shruti said...

Sir can you please help me with this program ie create menu containing buttons like insert, edit, delete and update in asp.net with c#

Unknown said...

its a very good code sir,mostly for billing application.
but if billing amount is in float value i.e. 125.50(one hundred twenty five rupees and fifty paise) so how it will be print.

Unknown said...

its a very good code sir,
but i want words to number
plz help me sir

Anonymous said...

behanchod khud se bhi kuch kar liya karo....sab kuch mai hi karunga kya...?????

Suresh Dasari

Anonymous said...

@vinay arora : abe chutiye

589652 = FIVE MILLION EIGHTY NINE THOUSAND SIX HUNDRED AND FIFTY TWO hoga

aisa tune kaun se class ki math me study kia hai ?

Kisi ki post ko wrong kehne se pehle khud ko verify to kar liya kar.


Suresh Sir , Please add a Reply Button for such stupid comments . otherwise these creepy peoples will never let others do something good and helpful.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.