In this post I will explain how to convert number into word
in asp.Net using c#.
Description:
I have one textbox , one label and one button control. When
you enter the number in textbox and click the button then it shows the word
according to number means you type 123
then it will display one hundred twenty three .
Design your page like
the code which given below.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="Convert"
onclick="Button1_Click"
/>
<br /><asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Code behind page which is given below.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public partial class NumberToWord : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
long num =Convert.ToInt64(TextBox1.Text);
string res = NumberToWords(num);
Label1.Text = res;
}
public static
string NumberToWords(long
number)
{
if (number == 0)
{
return
"Zero";
}
if(number < 0)
return
"Minus" + NumberToWords(Math.Abs(number)) + "
";
string
words = "";
if
((number / 100000000) > 0)
{
words += NumberToWords(number /
10000000) + " " + "Crore" + "
";
number %= 10000000;
}
if ((number / 1000000) > 0)
{
words += NumberToWords(number /
100000) + " " + "Lakh" + "
";
number %= 100000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number /
1000) + " " + "Thousand" + "
";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number /
100) + " " + "Hundred" + "
";
number %= 100;
}
if (number > 0)
{
if
(words != "")
words += "and"
+ " ";
var
unitW = new[] { "Zero",
"One", "Two",
"Three", "Four",
"Five", "Six",
"Seven", "Eight",
"Nine", "Ten",
"Eleven", "Tweleve",
"Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen",
"Nineteen" };
var
tenW = new[] { "Zero",
"Ten", "Twenty",
"Thirty", "Forty",
"Fivety", "Sixty",
"Seventy", "Eighty", "Ninety"
};
if
(number < 20)
words += unitW[number];
else
{
words+=tenW[number/10];
if
((number % 10) > 0)
{
words+=" "+unitW[number % 10];
}
}
}
return words;
}
}
Debug code and Run. If
you have any doubt regarding this post you can contact or give your comment.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.