Introduction : In this article I
am going to explain How to convert currency to words or we can say How to
convert number to words using asp.net with C# Language.
Description: It can convert up to 99
crores i.e. maximum 9 digit numbers. I have also set the max length properly of
the textbox to 9 so that you can enter up to 9 digits to avoid the exceptions. I
have also used the RequiredFieldValidator and RangeValidator validation controls
to ensure rupees/number that you want to convert in words can’t be left blank
and between 1 to 999999999.
In previous articles i explained How to Get age in years,months,days,hours and seconds from DOB and Get Title,Description and Keywords Meta tags from URL and Bind state categories and cities sub categories in single dropdownlist and Bind and implement Custom paging in asp.net DataList and Delete multiple records from asp.net gridview with checkbox selection and How to generate random number in asp.net ? and Get city, state and country based on zip code using Google map API in asp.net and How to read mac address using asp.net?
Implementation: Let's create an asp.net web application to see it in action.
Click on image to enlarge |
In previous articles i explained How to Get age in years,months,days,hours and seconds from DOB and Get Title,Description and Keywords Meta tags from URL and Bind state categories and cities sub categories in single dropdownlist and Bind and implement Custom paging in asp.net DataList and Delete multiple records from asp.net gridview with checkbox selection and How to generate random number in asp.net ? and Get city, state and country based on zip code using Google map API in asp.net and How to read mac address using asp.net?
- In the design page (.aspx) place two TextBox controls and a Button controls and design the web page as:
<fieldset style="width: 500PX;">
<legend>Convert Rupees/Numbers to words in Asp.net</legend>
<table>
<tr><td>Enter rupees in number: </td>
<td><asp:TextBox ID="txtNumber"
runat="server"
MaxLength="9"></asp:TextBox><asp:Button ID="btnConvert" runat="server" Text="Convert To Words"
onclick="btnConvert_Click" /><asp:RequiredFieldValidator ID="rfvNumber"
runat="server" ErrorMessage="Please enter rupees/number"
ControlToValidate="txtNumber"
Display="Dynamic"
ForeColor="#FF3300"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rngNumber"
runat="server"
ControlToValidate="txtNumber"
Display="Dynamic"
ErrorMessage="Please
enter rupees/number from 1 to 999999999"
ForeColor="#FF3300"
MaximumValue="999999999"
MinimumValue="1"
SetFocusOnError="True"
Type="Integer"></asp:RangeValidator>
</td>
</tr>
<tr>
<td>Converted Rupees in Words: </td>
<td><asp:TextBox ID="txtWords"
runat="server"
Rows="3"
ReadOnly="True"
TextMode="MultiLine"
Width="331px"></asp:TextBox></td>
</tr>
</table>
</fieldset>
C#.Net Code to convert Rupees,Currency or Numbers to words in Asp.net
- In the Code behind file (.aspx.cs) write the code as:
protected void btnConvert_Click(object
sender, EventArgs e)
{
Int64 NumVal = Convert.ToInt64(txtNumber.Text.Trim());
txtWords.Text = Rupees(NumVal);
}
public string Rupees(Int64 rup)
{
string result="";
Int64 res;
if(( rup / 10000000) > 0)
{
res
= rup / 10000000;
rup
= rup % 10000000;
result = result + ' ' +
RupeesToWords(res) + " Crore";
}
if(( rup / 100000) > 0)
{
res
= rup / 100000;
rup
= rup % 100000;
result = result + ' ' +
RupeesToWords(res) + " Lack";
}
if(( rup / 1000) > 0)
{
res
= rup / 1000;
rup
= rup % 1000;
result = result + ' ' +
RupeesToWords(res) + " Thousand";
}
if(( rup / 100) > 0)
{
res
= rup / 100;
rup
= rup % 100;
result = result + ' ' +
RupeesToWords(res) + " Hundred";
}
if ((rup % 10) >= 0)
{
res = rup % 100;
result = result + " " +
RupeesToWords(res);
}
result
= result + ' ' + "
Rupees only";
return result;
}
public string
RupeesToWords(Int64 rup)
{
string result="";
if ((rup >= 1) && (rup <= 10))
{
if ((rup % 10) == 1) result = "One";
if ((rup % 10) == 2) result = "Two";
if ((rup % 10) == 3) result = "Three";
if ((rup % 10) == 4) result = "Four";
if ((rup % 10) == 5) result = "Five";
if ((rup % 10) == 6) result = "Six";
if ((rup % 10) == 7) result = "Seven";
if ((rup % 10) == 8) result = "Eight";
if ((rup % 10) == 9) result = "Nine";
if ((rup % 10) == 0) result = "Ten";
}
if (rup > 9 && rup < 20)
{
if (rup == 11) result = "Eleven";
if (rup == 12) result = "Twelve";
if (rup == 13) result = "Thirteen";
if (rup == 14) result = "Forteen";
if (rup == 15) result = "Fifteen";
if (rup == 16) result = "Sixteen";
if (rup == 17) result = "Seventeen";
if (rup == 18) result = "Eighteen";
if (rup == 19) result = "Nineteen";
}
if (rup > 20 && (rup / 10) == 2 &&
(rup % 10) == 0) result = "Twenty";
if (rup > 20 && (rup / 10) == 3 &&
(rup % 10) == 0) result = "Thirty";
if (rup > 20 && (rup / 10) == 4 &&
(rup % 10) == 0) result = "Forty";
if (rup > 20 && (rup / 10) == 5 &&
(rup % 10) == 0) result = "Fifty";
if (rup > 20 && (rup / 10) == 6 &&
(rup % 10) == 0) result = "Sixty";
if (rup > 20 && (rup / 10) == 7 &&
(rup % 10) == 0) result = "Seventy";
if (rup > 20 && (rup / 10) == 8 &&
(rup % 10) == 0) result = "Eighty";
if (rup > 20 && (rup / 10) == 9 &&
(rup % 10) == 0) result = "Ninty";
if (rup > 20 && (rup / 10) == 2 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Twenty One";
if ((rup % 10) == 2) result = "Twenty Two";
if ((rup % 10) == 3) result = "Twenty Three";
if ((rup % 10) == 4) result = "Twenty Four";
if ((rup % 10) == 5) result = "Twenty Five";
if ((rup % 10) == 6) result = "Twenty Six";
if ((rup % 10) == 7) result = "Twenty Seven";
if ((rup % 10) == 8) result = "Twenty Eight";
if ((rup % 10) == 9) result = "Twenty Nine";
}
if (rup > 20 && (rup / 10) == 3 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Thirty One";
if ((rup % 10) == 2) result = "Thirty Two";
if ((rup % 10) == 3) result = "Thirty Three";
if ((rup % 10) == 4) result = "Thirty Four";
if ((rup % 10) == 5) result = "Thirty Five";
if ((rup % 10) == 6) result = "Thirty Six";
if ((rup % 10) == 7) result = "Thirty Seven";
if ((rup % 10) == 8) result = "Thirty Eight";
if ((rup % 10) == 9) result = "Thirty Nine";
}
if (rup > 20 && (rup / 10) == 4 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Forty One";
if ((rup % 10) == 2) result = "Forty Two";
if ((rup % 10) == 3) result = "Forty Three";
if ((rup % 10) == 4) result = "Forty Four";
if ((rup % 10) == 5) result = "Forty Five";
if ((rup % 10) == 6) result = "Forty Six";
if ((rup % 10) == 7) result = "Forty Seven";
if ((rup % 10) == 8) result = "Forty Eight";
if ((rup % 10) == 9) result = "Forty Nine";
}
if (rup > 20 && (rup / 10) == 5 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Fifty One";
if ((rup % 10) == 2) result = "Fifty Two";
if ((rup % 10) == 3) result = "Fifty Three";
if ((rup % 10) == 4) result = "Fifty Four";
if ((rup % 10) == 5) result = "Fifty Five";
if ((rup % 10) == 6) result = "Fifty Six";
if ((rup % 10) == 7) result = "Fifty Seven";
if ((rup % 10) == 8) result = "Fifty Eight";
if ((rup % 10) == 9) result = "Fifty Nine";
}
if (rup > 20 && (rup / 10) == 6 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Sixty One";
if ((rup % 10) == 2) result = "Sixty Two";
if ((rup % 10) == 3) result = "Sixty Three";
if ((rup % 10) == 4) result = "Sixty Four";
if ((rup % 10) == 5) result = "Sixty Five";
if ((rup % 10) == 6) result = "Sixty Six";
if ((rup % 10) == 7) result = "Sixty Seven";
if ((rup % 10) == 8) result = "Sixty Eight";
if ((rup % 10) == 9) result = "Sixty Nine";
}
if (rup > 20 && (rup / 10) == 7 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Seventy One";
if ((rup % 10) == 2) result = "Seventy Two";
if ((rup % 10) == 3) result = "Seventy Three";
if ((rup % 10) == 4) result = "Seventy Four";
if ((rup % 10) == 5) result = "Seventy Five";
if ((rup % 10) == 6) result = "Seventy Six";
if ((rup % 10) == 7) result = "Seventy Seven";
if ((rup % 10) == 8) result = "Seventy Eight";
if ((rup % 10) == 9) result = "Seventy Nine";
}
if (rup > 20 && (rup / 10) == 8 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Eighty One";
if ((rup % 10) == 2) result = "Eighty Two";
if ((rup % 10) == 3) result = "Eighty Three";
if ((rup % 10) == 4) result = "Eighty Four";
if ((rup % 10) == 5) result = "Eighty Five";
if ((rup % 10) == 6) result = "Eighty Six";
if ((rup % 10) == 7) result = "Eighty Seven";
if ((rup % 10) == 8) result = "Eighty Eight";
if ((rup % 10) == 9) result = "Eighty Nine";
}
if (rup > 20 && (rup / 10) == 9 &&
(rup % 10) != 0)
{
if ((rup % 10) == 1) result = "Ninty One";
if ((rup % 10) == 2) result = "Ninty Two";
if ((rup % 10) == 3) result = "Ninty Three";
if ((rup % 10) == 4) result = "Ninty Four";
if ((rup % 10) == 5) result = "Ninty Five";
if ((rup % 10) == 6) result = "Ninty Six";
if ((rup % 10) == 7) result = "Ninty Seven";
if ((rup % 10) == 8) result = "Ninty Eight";
if ((rup % 10) == 9) result = "Ninty Nine";
}
return result;
}
Now over to you:
"If you like my work; you can appreciate by leaving your comments,
hitting Facebook like button, following on Google+, Twitter, Linked in and
Pinterest, stumbling my posts on stumble upon and subscribing for receiving
free updates directly to your inbox . Stay tuned for more technical
updates."
3 comments
Click here for commentsPlease modify the code to as below...it is not counting the 10's value
ReplyEx-110 -> One Hendred only
if ((rup % 10) >= 0)
{
res = rup % 100;
result = result + " " + RupeesToWords(res);
}
Thanks for notifying the problem and your suggestions. I have made the updation.. its working perfectly now.
ReplyWow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks money converter
ReplyIf you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..