how to convert number to text currency in C#

Somnetimes we will get a requirement like converting a number into words. such as: 568.25 = Five Hundred Sixty Eight and Paisa Twenty Five Only.

Create a converter class with that code:

Example:
568.25 = Five Hundred Sixty Eight Only.
568.25 = Five Hundred Sixty Eight and Paisa Twenty Five Only.

[code lang=”js”]
public class converter
{
static Tuple<int, string>[] powers =
{
new Tuple<int, string>(0, “”),
new Tuple<int, string>(3, “Thousand”),
new Tuple<int, string>(5, “Lac”),
new Tuple<int, string>(7, “Crore”)
};

//this array specifies the digits’ names
static string[] digits = { “”, “One”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine” };
static string[] extendedDigits = { “Ten”, “Eleven”, “Twelve”, “Thirteen”, “Fourteen”, “Fifteen”, “Sixteen”, “Seventeen”, “Eighteen”, “Nineteen” };
static string[] tensWords = { “”, “”, “Twenty”, “Thirty”, “Forty”, “Fifty”, “Sixty”, “Seventy”, “Eighty”, “Ninety” };

public static string NumberToWords(decimal number)
{

var sb = new StringBuilder();

//sb.Append(“Taka “);

//begin with the left most digit (greatest power of 10)
for (int i = powers.Length – 1; i >= 0; –i)
{
//translate the current part only (for a known power of 10)
//usually part is a 3-digit number
int part = (int)(number / (long)Math.Pow(10, powers[i].Item1));
//if the part is 0, we don’t have to add anything
if (part > 0)
{
//extract the hundreds
int hundreds = part / 100;
if (hundreds > 9)
throw new ArgumentException(number + ” is too large and cannot be expressed.”);
if (hundreds > 0)
{
//if there are hundreds, copy them to the output
sb.Append(digits[hundreds]);
sb.Append(” Hundred “);
}
//convert the next two digits
sb.Append(TwoDigitNumberToWord(part % 100));
sb.Append(” “);
//and append the name of the power of 10
sb.Append(powers[i].Item2);
sb.Append(” “);
//subtract the currently managed part
number -= part * (long)Math.Pow(10, powers[i].Item1);
}
}

string sNumStr = String.Format(“{0:0.00}”, number);

string sPaisa = TwoDigitNumberToWord(Convert.ToInt32(sNumStr.Substring(sNumStr.Length – 2, 2)));

if (sPaisa.Length != 0)
{
sPaisa = “Paisa ” + sPaisa + ” Only”;

sb.Length–;
//sb.Length–;
sb.Append(” and “);
sb.Append(sPaisa);
}
else
{
sb.Length–;
//sb.Length–;
sb.Append(” Only”);
}

return sb.ToString();
}

}

[/code]

Using the class

[code lang=”js”]
string result = converter.NumberToWords(568.25m);
[/code]

public class converter
{
static Tuple<int, string>[] powers =
{
new Tuple<int, string>(0, ""),
new Tuple<int, string>(3, "Thousand"),
new Tuple<int, string>(5, "Lac"),
new Tuple<int, string>(7, "Crore")
};

//this array specifies the digits' names
static string[] digits = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
static string[] extendedDigits = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
static string[] tensWords = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

public static string NumberToWords(decimal number)
{
var sb = new StringBuilder();
//begin with the left most digit (greatest power of 10)
	for (int i = powers.Length - 1; i >= 0; --i)
	{
	//translate the current part only (for a known power of 10)
	//usually part is a 3-digit number
	int part = (int)(number / (long)Math.Pow(10, powers[i].Item1));
	//if the part is 0, we don't have to add anything
	if (part > 0)
		{
		//extract the hundreds
		int hundreds = part / 100;
		if (hundreds > 9)
			throw new ArgumentException(number + " is too large and cannot be expressed.");
		if (hundreds > 0)
			{
			//if there are hundreds, copy them to the output
			sb.Append(digits[hundreds]);
			sb.Append(" Hundred ");
			}
//convert the next two digits
sb.Append(TwoDigitNumberToWord(part % 100));
sb.Append(" ");
//and append the name of the power of 10
sb.Append(powers[i].Item2);
sb.Append(" ");
//subtract the currently managed part
number -= part * (long)Math.Pow(10, powers[i].Item1);
}
}

string sNumStr = String.Format("{0:0.00}", number);

string sPaisa = TwoDigitNumberToWord(Convert.ToInt32(sNumStr.Substring(sNumStr.Length - 2, 2)));

if (sPaisa.Length != 0)
{
sPaisa = "Paisa " + sPaisa + " Only";

sb.Length--;
//sb.Length--;
sb.Append(" and ");
sb.Append(sPaisa);
}
else
{
sb.Length--;
//sb.Length--;
sb.Append(" Only");
}

return sb.ToString();
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *