Here is a C# 2.0 version (it can easily be converted to C# 1.0 by removing the static attribute on the class and adding a private default constructor):
using System;
namespace Utility { /// <summary> /// The CurrencyToEnglish class converts a currency value to an english representation. /// </summary> public static class CurrencyToEnglish { /* * The original class was written in Managed C++ and found on * http://www.thecodeproject.com/useritems/Convert_Currency_to_Words.asp */ private static int SearchStr(string sStr, string cStr) { return sStr.IndexOf(cStr); }
private static string Left(string numStr, int index) { return numStr.Substring(0, index); }
private static string Right(string numStr, int index) { if(numStr.Length < index) index = numStr.Length; return numStr.Substring(numStr.Length - index); }
private static string Mid(string numStr, int start, int length) { return numStr.Substring(start, length); }
private static string ConvertHundreds(string strNum) { string result = ""; if (System.Convert.ToDecimal(strNum) == 0) return ""; strNum = Right(string.Concat("000", strNum), 3); if (string.Compare(Left(strNum,1), "0") != 0) { result = string.Concat(ConvertDigit(Left(strNum, 1)), " Hundred "); } if (string.Compare(Mid(strNum, 1, 1), "0") !=0) { result += ConvertTens(Mid(strNum, 1, 2)); } else { result += ConvertDigit(Mid(strNum, 2, 1)); } return result; }
private static string ConvertTens(string strTens) { string sTens = ""; if (System.Convert.ToInt16(Left(strTens, 1)) == 1) { int nTens = System.Convert.ToInt16(strTens); switch (nTens) { case 10: sTens = "Ten"; break; case 11: sTens = "Eleven"; break; case 12: sTens = "Twelve"; break; case 13: sTens = "Thirteen"; break; case 14: sTens = "Fourteen"; break; case 15: sTens = "Fifteen"; break; case 16: sTens = "Sixteen"; break; case 17: sTens = "Seventeen"; break; case 18: sTens = "Eighteen"; break; case 19: sTens = "Nineteen"; break; } } else { switch (System.Convert.ToInt16(Left(strTens, 1))) { case 2: sTens = "Twenty "; break; case 3: sTens = "Thirty "; break; case 4: sTens = "Fourty "; break; case 5: sTens = "Fifty "; break; case 6: sTens = "Sixty "; break; case 7: sTens = "Seventy "; break; case 8: sTens = "Eighty "; break; case 9: sTens = "Ninety "; break; } sTens = string.Concat(sTens, ConvertDigit(Right(strTens, 1))); } return sTens; }
private static string ConvertDigit(string strDigit) { string sDigit; int nDigit = System.Convert.ToInt16(strDigit); switch (nDigit) { case 1: sDigit = "One"; break; case 2: sDigit = "Two"; break; case 3: sDigit = "Three"; break; case 4: sDigit = "Four"; break; case 5: sDigit = "Five"; break; case 6: sDigit = "Six"; break; case 7: sDigit = "Seven"; break; case 8: sDigit = "Eight"; break; case 9: sDigit = "Nine"; break; default: sDigit = ""; break; } return sDigit; } /// <summary> /// This function will convert a currency to its word representation. /// </summary> /// <param name="number">Source currency amount.</param> /// <param name="capitalize">Capitalizes the first word in the string.</param> /// <param name="includeDollarVerbiage">If true outputs "X dollars and Y cents".</param> /// <param name="allCaps">Converts the string to UPPER case. Takes precedence over <paramref name="capitalize" />.</param> /// <returns>The amount in English.</returns> public static string ConvertCurrencyToEnglish(decimal amount, bool capitalize, bool includeDollarVerbiage, bool allCaps) { string temp = ""; string dollars = ""; string cents = ""; int count = 1; int decimalPlace; string number = amount.ToString();
decimalPlace = SearchStr(number, "."); if (decimalPlace > 0 && number.Length == decimalPlace + 1) number = Left(number, decimalPlace); if (decimalPlace > 0 && number.Length > decimalPlace + 1) { temp = Left(string.Concat(Mid(number, decimalPlace + 1, number.Length - (decimalPlace + 1)), "00"), 2); cents = ConvertTens(temp); number = Left(number, decimalPlace); } string[] place = {"", "", " Thousand ", " Million ", " Billion ", " Trillion ", " Quadrillion ", " Quintillion ", " Sextillion "}; while (number != "") { temp = ConvertHundreds(Right(number, 3)); if (temp != "") { dollars = string.Concat(string.Concat(temp, place[count]), dollars); } number = number.Length > 3 ? Left(number, number.Length - 3) : ""; count++; }
if (includeDollarVerbiage) { if (dollars.Length == 0) dollars = "Zero Dollars"; else if (dollars == "One") dollars = "One Dollar"; else dollars += " Dollars";
if (cents.Length == 0) cents = " And Zero Cents"; else if (cents == "One") cents = " And One Cent"; else cents = string.Concat(" And ", cents, " Cents "); } else { if (cents.Length > 0) cents = " Point " + cents; }
string result = dollars + cents; result = result.Replace(" ", " "); if (allCaps) result = result.ToUpper(); else { if (capitalize) result = string.Concat(result.Substring(0, 1).ToUpper(), result.Substring(1, result.Length - 1).ToLower()); else result = result.ToLower(); } return result; }
/// <summary> /// This function will convert a currency to its word representation. /// </summary> /// <param name="amount">The amount.</param> /// <param name="capitalize">Capitalizes the first word in the string.</param> /// <returns>The amount in English.</returns> public static string ConvertCurrencyToEnglish(decimal amount, bool capitalize) { return ConvertCurrencyToEnglish(amount, capitalize, true, false); } } }
Thanks for the original post. Enjoy.
|