# How do i convert Rupees in words using c# [Answered]RSS

## 7 replies

Last post Oct 24, 2009 01:29 AM by debugmm

None

0 Points

6 Posts

### How do i convert Rupees in words using c#

Hi Everyone....

I have task to convert rupees in words upto 10 lakhs using c#. During calculation i want to show the full amount as well as in words.Please do help me...Thanx In advance..:):)

All-Star

39523 Points

8946 Posts

### Re: How do i convert Rupees in words using c#

Jun 19, 2009 01:12 PM|rtpHarry|LINK

Forgive my international ignorance but I am not sure what lakhs is - I put it into google and this came up:

Participant

823 Points

308 Posts

### Re: How do i convert Rupees in words using c#

Jun 22, 2009 11:32 AM|ravi2k7@hotmail.com|LINK

HI

http://www.codeproject.com/useritems/codesamples.asp

or

else

using System;

namespace custom.util

{

public class NumberToEnglish

{

public String changeNumericToWords(double numb)

{

String num = numb.ToString();return changeToWords(num, false);

}

public String changeCurrencyToWords(String numb)

{

return changeToWords(numb, true);

}

public String changeNumericToWords(String numb)

{

return changeToWords(numb, false);

}

public String changeCurrencyToWords(double numb)

{

return changeToWords(numb.ToString(), true);

}

private String changeToWords(String numb, bool isCurrency)

{

String val = "", wholeNo = numb, points = "", andStr = "", pointStr="";

String endStr = (isCurrency) ? ("Only") : ("");

try

{

int decimalPlace = numb.IndexOf(".");if (decimalPlace > 0)

{

wholeNo = numb.Substring(0, decimalPlace);

points = numb.Substring(decimalPlace+1);

if (Convert.ToInt32(points) > 0)

{

andStr = (isCurrency)?("and"):("point");// just to separate whole numbers from points/cents

endStr = (isCurrency) ? ("Cents "+endStr) : ("");

pointStr = translateCents(points);

}

}

val =
String.Format("{0} {1}{2} {3}",translateWholeNumber(wholeNo).Trim(),andStr,pointStr,endStr);

}

catch { ;}return val;

}

private String translateWholeNumber(String number)

{

string word = "";

try

{

bool beginsZero = false;//tests for 0XX

bool isDone = false;//test if already translated

double dblAmt = (Convert.ToDouble(number));

//if ((dblAmt > 0) && number.StartsWith("0"))

if (dblAmt > 0)

{//test for zero or digit zero in a nuemric

beginsZero = number.StartsWith("0");

int numDigits = number.Length;

int pos = 0;//store digit grouping

String place = "";//digit grouping name:hundres,thousand,etc...

switch (numDigits)

{

case 1://ones' range

word = ones(number);

isDone =
true;

break;

case 2://tens' range

word = tens(number);

isDone =
true;

break;

case 3://hundreds' range

pos = (numDigits % 3) + 1;

place =
" Hundred ";

break;

case 4://thousands' range

case 5:case 6:

pos = (numDigits % 4) + 1;

place = " Thousand ";

break;

case 7://millions' range

case 8:case 9:

pos = (numDigits % 7) + 1;

place = " Million ";

break;

case 10://Billions's range

pos = (numDigits % 10) + 1;

place =
" Billion ";

break;

//add extra case options for anything above Billion...

default:

isDone = true;

break;

}

if (!isDone)

{//if transalation is not done, continue...(Recursion comes in now!!)

word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));

//check for trailing zeros

if (beginsZero) word = " and " + word.Trim();

}

//ignore digit grouping names

if (word.Trim().Equals(place.Trim())) word = "";

}

}

catch { ;}return word.Trim();

}

private String tens(String digit)

{

int digt = Convert.ToInt32(digit);

String name = null;switch (digt)

{

case 10:

name = "Ten";

break;

case 11:

name = "Eleven";

break;

case 12:

name = "Twelve";

break;

case 13:

name = "Thirteen";

break;

case 14:

name = "Fourteen";

break;

case 15:

name = "Fifteen";

break;

case 16:

name = "Sixteen";

break;

case 17:

name = "Seventeen";

break;

case 18:

name = "Eighteen";

break;

case 19:

name = "Nineteen";

break;

case 20:

name = "Twenty";

break;

case 30:

name = "Thirty";

break;

case 40:

name = "Fourty";

break;

case 50:

name = "Fifty";

break;

case 60:

name = "Sixty";

break;

case 70:

name = "Seventy";

break;

case 80:

name = "Eighty";

break;

case 90:

name = "Ninety";

break;

default:

if (digt > 0)

{

name = tens(digit.Substring(0, 1) +
"0") + " " + ones(digit.Substring(1));

}

break;

}

return name;

}

private String ones(String digit)

{

int digt = Convert.ToInt32(digit);

String name = "";

switch (digt)

{

case 1:

name = "One";

break;

case 2:

name = "Two";

break;

case 3:

name = "Three";

break;

case 4:

name = "Four";

break;

case 5:

name = "Five";

break;

case 6:

name = "Six";

break;

case 7:

name = "Seven";

break;

case 8:

name = "Eight";

break;

case 9:

name = "Nine";

break;

}

return name;

}

private String translateCents(String cents)

{

String cts = "", digit = "", engOne = "";for (int i = 0; i < cents.Length; i++)

{

digit = cents[i].ToString();

if (digit.Equals("0"))

{

engOne =
"Zero";

}

else

{

engOne = ones(digit);

}

cts +=
" " + engOne;

}

return cts;

}

}

}

reference

Regards
Ravi
Please click "mark as answer" if this post helped you.

None

0 Points

6 Posts

### Re: How do i convert Rupees in words using c#

Hey Guys...

I have got the solution upto 99 crores...

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

Label1.Text = rupees(Convert.ToInt64(TextBox1.Text));

}
public string rupees(Int64 rupees)
{
string result="";
Int64 res;
if(( rupees / 10000000) > 0)
{
res =  rupees / 10000000;
rupees =  rupees % 10000000;
result = result + ' ' + rupeestowords(res) + " Crore";
}
if(( rupees / 100000) > 0)
{
res =  rupees / 100000;
rupees =  rupees % 100000;
result = result + ' ' + rupeestowords(res) + " Lack";
}
if(( rupees / 1000) > 0)
{
res =  rupees / 1000;
rupees =  rupees % 1000;
result = result + ' ' + rupeestowords(res) + " Thousand";
}
if(( rupees / 100) > 0)
{
res =  rupees / 100;
rupees =  rupees % 100;
result = result + ' ' + rupeestowords(res) + " Hundred";
}
if ((rupees % 10) > 0)
{
res = rupees % 100;
result = result + " " + rupeestowords(res);
}
result = result + ' ' + " Rupees only";
return result;
}
public string rupeestowords(Int64 rupees)
{
string result="";
if ((rupees >= 1) && (rupees <= 10))
{
if ((rupees % 10) == 1) result = "One";
if ((rupees % 10) == 2) result = "Two";
if ((rupees % 10) == 3) result = "Three";
if ((rupees % 10) == 4) result = "Four";
if ((rupees % 10) == 5) result = "Five";
if ((rupees % 10) == 6) result = "Six";
if ((rupees % 10) == 7) result = "Seven";
if ((rupees % 10) == 8) result = "Eight";
if ((rupees % 10) == 9) result = "Nine";
if ((rupees % 10) == 0) result = "Ten";
}
if (rupees > 9 && rupees < 20)
{
if (rupees == 11) result = "Eleven";
if (rupees == 12) result = "Twelve";
if (rupees == 13) result = "Thirteen";
if (rupees == 14) result = "Forteen";
if (rupees == 15) result = "Fifteen";
if (rupees == 16) result = "Sixteen";
if (rupees == 17) result = "Seventeen";
if (rupees == 18) result = "Eighteen";
if (rupees == 19) result = "Nineteen";
}
if (rupees > 20 && (rupees / 10) == 2 && (rupees % 10) == 0) result = "Twenty";
if (rupees > 20 && (rupees / 10) == 3 && (rupees % 10) == 0) result = "Thirty";
if (rupees > 20 && (rupees / 10) == 4 && (rupees % 10) == 0) result = "Forty";
if (rupees > 20 && (rupees / 10) == 5 && (rupees % 10) == 0) result = "Fifty";
if (rupees > 20 && (rupees / 10) == 6 && (rupees % 10) == 0) result = "Sixty";
if (rupees > 20 && (rupees / 10) == 7 && (rupees % 10) == 0) result = "Seventy";
if (rupees > 20 && (rupees / 10) == 8 && (rupees % 10) == 0) result = "Eighty";
if (rupees > 20 && (rupees / 10) == 9 && (rupees % 10) == 0) result = "Ninty";

if (rupees > 20 && (rupees / 10) == 2 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Twenty One";
if ((rupees % 10) == 2) result = "Twenty Two";
if ((rupees % 10) == 3) result = "Twenty Three";
if ((rupees % 10) == 4) result = "Twenty Four";
if ((rupees % 10) == 5) result = "Twenty Five";
if ((rupees % 10) == 6) result = "Twenty Six";
if ((rupees % 10) == 7) result = "Twenty Seven";
if ((rupees % 10) == 8) result = "Twenty Eight";
if ((rupees % 10) == 9) result = "Twenty Nine";
}
if (rupees > 20 && (rupees / 10) == 3 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Thirty One";
if ((rupees % 10) == 2) result = "Thirty Two";
if ((rupees % 10) == 3) result = "Thirty Three";
if ((rupees % 10) == 4) result = "Thirty Four";
if ((rupees % 10) == 5) result = "Thirty Five";
if ((rupees % 10) == 6) result = "Thirty Six";
if ((rupees % 10) == 7) result = "Thirty Seven";
if ((rupees % 10) == 8) result = "Thirty Eight";
if ((rupees % 10) == 9) result = "Thirty Nine";
}
if (rupees > 20 && (rupees / 10) == 4 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Forty One";
if ((rupees % 10) == 2) result = "Forty Two";
if ((rupees % 10) == 3) result = "Forty Three";
if ((rupees % 10) == 4) result = "Forty Four";
if ((rupees % 10) == 5) result = "Forty Five";
if ((rupees % 10) == 6) result = "Forty Six";
if ((rupees % 10) == 7) result = "Forty Seven";
if ((rupees % 10) == 8) result = "Forty Eight";
if ((rupees % 10) == 9) result = "Forty Nine";
}
if (rupees > 20 && (rupees / 10) == 5 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Fifty One";
if ((rupees % 10) == 2) result = "Fifty Two";
if ((rupees % 10) == 3) result = "Fifty Three";
if ((rupees % 10) == 4) result = "Fifty Four";
if ((rupees % 10) == 5) result = "Fifty Five";
if ((rupees % 10) == 6) result = "Fifty Six";
if ((rupees % 10) == 7) result = "Fifty Seven";
if ((rupees % 10) == 8) result = "Fifty Eight";
if ((rupees % 10) == 9) result = "Fifty Nine";
}
if (rupees > 20 && (rupees / 10) == 6 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Sixty One";
if ((rupees % 10) == 2) result = "Sixty Two";
if ((rupees % 10) == 3) result = "Sixty Three";
if ((rupees % 10) == 4) result = "Sixty Four";
if ((rupees % 10) == 5) result = "Sixty Five";
if ((rupees % 10) == 6) result = "Sixty Six";
if ((rupees % 10) == 7) result = "Sixty Seven";
if ((rupees % 10) == 8) result = "Sixty Eight";
if ((rupees % 10) == 9) result = "Sixty Nine";
}
if (rupees > 20 && (rupees / 10) == 7 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Seventy One";
if ((rupees % 10) == 2) result = "Seventy Two";
if ((rupees % 10) == 3) result = "Seventy Three";
if ((rupees % 10) == 4) result = "Seventy Four";
if ((rupees % 10) == 5) result = "Seventy Five";
if ((rupees % 10) == 6) result = "Seventy Six";
if ((rupees % 10) == 7) result = "Seventy Seven";
if ((rupees % 10) == 8) result = "Seventy Eight";
if ((rupees % 10) == 9) result = "Seventy Nine";
}
if (rupees > 20 && (rupees / 10) == 8 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Eighty One";
if ((rupees % 10) == 2) result = "Eighty Two";
if ((rupees % 10) == 3) result = "Eighty Three";
if ((rupees % 10) == 4) result = "Eighty Four";
if ((rupees % 10) == 5) result = "Eighty Five";
if ((rupees % 10) == 6) result = "Eighty Six";
if ((rupees % 10) == 7) result = "Eighty Seven";
if ((rupees % 10) == 8) result = "Eighty Eight";
if ((rupees % 10) == 9) result = "Eighty Nine";
}
if (rupees > 20 && (rupees / 10) == 9 && (rupees % 10) != 0)
{
if ((rupees % 10) == 1) result = "Ninty One";
if ((rupees % 10) == 2) result = "Ninty Two";
if ((rupees % 10) == 3) result = "Ninty Three";
if ((rupees % 10) == 4) result = "Ninty Four";
if ((rupees % 10) == 5) result = "Ninty Five";
if ((rupees % 10) == 6) result = "Ninty Six";
if ((rupees % 10) == 7) result = "Ninty Seven";
if ((rupees % 10) == 8) result = "Ninty Eight";
if ((rupees % 10) == 9) result = "Ninty Nine";
}
return result;
}
}

All-Star

39523 Points

8946 Posts

### Re: How do i convert Rupees in words using c#

Jun 25, 2009 08:45 PM|rtpHarry|LINK

Hey Guys...

I have got the solution upto 99 crores...

... snip ...

Ouch! Thats some long code.

FYI Ninety is spelled wrong in your example.

None

0 Points

6 Posts

### Re: How do i convert Rupees in words using c#

Dude.....

Thanx for mentioning the correction...But i feel Spelling is not a big deal.....just enjoy with the solution....:):)

Star

9355 Points

2710 Posts

### Re: How do i convert Rupees in words using c#

Jun 26, 2009 07:13 AM|Paul Linton|LINK

I am glad that you liked your own code so much that you marked it as the answer but have you tried running it?  It doesn't work for 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 110, 120, ... (only a 10% error rate )

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.

Member

20 Points

4 Posts

### Re: How do i convert Rupees in words using c#

Oct 24, 2009 01:29 AM|debugmm|LINK

Thanks for your code.

Some modification made to suite INR and Tens support after decimal.

```class CurrencyToWordINR
{
public String changeNumericToWords(double numb)
{
String num = numb.ToString(); return changeToWords(num, false);

}
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb, true);

}
public String changeNumericToWords(String numb)
{
return changeToWords(numb, false);

}
public String changeCurrencyToWords(double numb)
{
return changeToWords(numb.ToString(), true);

}
private String changeToWords(String numb, bool isCurrency)
{

String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
String endStr = (isCurrency) ? ("Only") : ("");

try
{
int decimalPlace = numb.IndexOf("."); if (decimalPlace > 0)
{

wholeNo = numb.Substring(0, decimalPlace);

points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt32(points) > 0)
{

andStr = (isCurrency) ? ("and") : ("point");// just to separate whole numbers from points/cents
endStr = (isCurrency) ? ("Paisa " + endStr) : ("");

pointStr = translateCents(points);

}

}
val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);

}
catch { ;} return val;

}
private String translateWholeNumber(String number)
{

string word = "";

try
{

bool beginsZero = false;//tests for 0XX

bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));

//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0");

int numDigits = number.Length;

int pos = 0;//store digit grouping

String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{

case 1://ones' range

word = ones(number);
isDone = true;

break;

case 2://tens' range

word = tens(number);
isDone = true;

break;

case 3://hundreds' range

pos = (numDigits % 3) + 1;
place = " Hundred ";

break;

case 4://thousands' range
case 5:

pos = (numDigits % 4) + 1;

place = " Thousand ";
break;

case 6://Lakhs' range
case 7:

pos = (numDigits % 6) + 1;

place = " Lakh ";
break;

case 8://Crores' range
case 9:

pos = (numDigits % 8) + 1;

place = " Crore ";
break;

case 10://Arabs range
case 11:

pos = (numDigits % 10) + 1;
place = " Arab ";

break;

//add extra case options for anything above Billion...
default:

isDone = true;
break;

}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)

word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));

//check for trailing zeros
if (beginsZero) word = " and " + word.Trim();

}

//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";

}

String Result = word.Trim();
Result = Result.Replace("and Hundred", "");
Result = Result.Replace("and Thousand", "");
Result = Result.Replace("and Lakh", "");
Result = Result.Replace("and Crore", "");
Result = Result.Replace(" and ", " ");

word = Result;
}
catch { ;} return word.Trim();

}
private String tens(String digit)
{

int digt = Convert.ToInt32(digit);
String name = null; switch (digt)
{

case 10:
name = "Ten";

break;
case 11:

name = "Eleven";
break;

case 12:
name = "Twelve";

break;
case 13:

name = "Thirteen";
break;

case 14:
name = "Fourteen";

break;
case 15:

name = "Fifteen";
break;

case 16:
name = "Sixteen";

break;
case 17:

name = "Seventeen";
break;

case 18:
name = "Eighteen";

break;
case 19:

name = "Nineteen";
break;

case 20:
name = "Twenty";

break;
case 30:

name = "Thirty";
break;

case 40:
name = "Fourty";

break;
case 50:

name = "Fifty";
break;

case 60:
name = "Sixty";

break;
case 70:

name = "Seventy";
break;

case 80:
name = "Eighty";

break;
case 90:

name = "Ninety";
break;

default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));

}
break;

}
return name;

}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);

String name = "";
switch (digt)
{
case 1:

name = "One";
break;

case 2:
name = "Two";

break;
case 3:

name = "Three";
break;

case 4:
name = "Four";

break;
case 5:

name = "Five";
break;

case 6:
name = "Six";

break;
case 7:

name = "Seven";
break;

case 8:
name = "Eight";

break;
case 9:

name = "Nine";
break;

}
return name;

}
private String translateCents(String cents)
{
String cts = "", digit = "", engOne = "";
for (int i = 0; i < cents.Length; i++)
{
digit = cents[i].ToString();
if (digit.Equals("0"))
{
engOne = "Zero";
}
else
{
engOne = ones(digit);
}
cts += " " + engOne;
if (i == 1)
{
if (Convert.ToInt32(cents) > 9 && Convert.ToInt32(cents) < 21)
{
cts = " " + tens(cents);
}
else
{
digit = cents[0].ToString();
cts = " " + tens(digit+"0");
digit = cents[1].ToString();
cts += " " + ones(digit);
}
}
}
return cts;
}
}```

C# code .net 2.0