<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default2.aspx.cs"
Inherits="Default2"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<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>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 :
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;
}
}
No comments:
Post a Comment