Convert Number(Rupees)into words using C# in Asp.Net
Submit the input on rupees textbox,when you click Rupees in words textbox means it will convert the input number into words.
Note:Friends don't forget to use AutoPostBack="true",where you use OnTextChanged in the Textbox.
Source code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvNum2Word.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap-theme.min.css" rel="stylesheet" />
<link href="Content/bootstrap-theme.css" rel="stylesheet" />
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body style="background-color:hotpink">
<form id="form1" runat="server">
<div class="col-lg-12 center-block">
<h3 style="text-align:center;">Sample Receipt</h3>
<div>
<div class="col-lg-2"></div>
<div class="col-lg-8">
<table class="table table-condensed" style="text-align:center;border:2px groove; border-radius:5px">
<tr>
<td>
<div>
<asp:Label ID="Label2" runat="server" Text="Date"></asp:Label>
</div>
</td>
<td>
<div>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Date"></asp:TextBox>
</div>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Account No"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Rupees" runat="server" Text="Rupees"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" OnTextChanged="TextBox4_TextChanged" TextMode="Number" AutoPostBack="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Rupees(in words)"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
</td>
</tr>
</table>
</div>
<div class="col-lg-2"></div>
</div>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConvNum2Word
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string NumbersToWords(int inputNumber)
{
int inputNo = inputNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (inputNo < 0)
{
sb.Append("Minus ");
inputNo = -inputNo;
}
string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i == 0) sb.Append("Only ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
int inpno = Convert.ToInt32(TextBox4.Text);
string outwords = NumbersToWords(inpno);
TextBox5.Text = outwords;
}
}
}
Result
Fig(1)Convert Number(rupees)into words.
Hello! Friends,you can download the file from here: Download
Submit the input on rupees textbox,when you click Rupees in words textbox means it will convert the input number into words.
Note:Friends don't forget to use AutoPostBack="true",where you use OnTextChanged in the Textbox.
Source code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ConvNum2Word.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap-theme.min.css" rel="stylesheet" />
<link href="Content/bootstrap-theme.css" rel="stylesheet" />
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body style="background-color:hotpink">
<form id="form1" runat="server">
<div class="col-lg-12 center-block">
<h3 style="text-align:center;">Sample Receipt</h3>
<div>
<div class="col-lg-2"></div>
<div class="col-lg-8">
<table class="table table-condensed" style="text-align:center;border:2px groove; border-radius:5px">
<tr>
<td>
<div>
<asp:Label ID="Label2" runat="server" Text="Date"></asp:Label>
</div>
</td>
<td>
<div>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Date"></asp:TextBox>
</div>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Account No"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Rupees" runat="server" Text="Rupees"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" OnTextChanged="TextBox4_TextChanged" TextMode="Number" AutoPostBack="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Rupees(in words)"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
</td>
</tr>
</table>
</div>
<div class="col-lg-2"></div>
</div>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConvNum2Word
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string NumbersToWords(int inputNumber)
{
int inputNo = inputNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (inputNo < 0)
{
sb.Append("Minus ");
inputNo = -inputNo;
}
string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i == 0) sb.Append("Only ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
int inpno = Convert.ToInt32(TextBox4.Text);
string outwords = NumbersToWords(inpno);
TextBox5.Text = outwords;
}
}
}
Result
Fig(1)Convert Number(rupees)into words.
Hello! Friends,you can download the file from here: Download
Thanks for Visiting...