Convert Money to Text in C Sharp

From CDYNE Wiki
Jump to navigation Jump to search

This C# code allows you to convert currency numbers into words.

Example: $152.17 into One Hundred Fifty Two Dollars and Seventeen Cents.

  1 		public static string ConvertMoneyToText(string value)
  2 		{
  3 			value = value.Replace(",", "").Replace("$", "");
  4 			int decimalCount = 0;
  5 			int Val = value.Length - 1;
  6 			for (int x = 0; x <= Val; x++)
  7 			{
  8 				char Val2 = value[x];
  9 				if (Val2.ToString() == ".")
 10 				{
 11 					decimalCount++;
 12 					if (decimalCount > 1)
 13 					{
 14 						throw new ArgumentException("Only monetary values are accepted");
 15 					}
 16 				}
 17 				Val2 = value[x];
 18 				char Valtemp = value[x];
 19 				if (!(char.IsDigit(value[x]) | (Val2.ToString() == ".")) & !((x == 0) & (Valtemp.ToString() == "-")))
 20 				{
 21 					throw new ArgumentException("Only monetary values are accepted");
 22 				}
 23 			}
 24 			string returnValue = "";
 25 			string[] parts;
 26 			if (value.Contains("."))
 27 				parts = value.Split(new char[] { '.' });
 28 			else
 29 				parts = (value + ".00").Split(new char[] { '.' });
 30 
 31 
 32 			parts[1] = new string((parts[1] + "00").Substring(0, 2).ToCharArray());
 33 			bool IsNegative = parts[0].Contains("-");
 34 			if (parts[0].Replace("-", "").Length > 0x12)
 35 			{
 36 				throw new ArgumentException("Maximum value is $999,999,999,999,999,999.99");
 37 			}
 38 			if (IsNegative)
 39 			{
 40 				parts[0] = parts[0].Replace("-", "");
 41 				returnValue = returnValue + "Minus ";
 42 			}
 43 			if (parts[0].Length > 15)
 44 			{
 45 				returnValue = ((((returnValue + HundredsText(parts[0].PadLeft(0x12, '0').Substring(0, 3)) + "Quadrillion ")
 46 					+ HundredsText(parts[0].PadLeft(0x12, '0').Substring(3, 3)) + "Trillion ") +
 47 					HundredsText(parts[0].PadLeft(0x12, '0').Substring(6, 3)) + "Billion ") +
 48 					HundredsText(parts[0].PadLeft(0x12, '0').Substring(9, 3)) + "Million ") +
 49 					HundredsText(parts[0].PadLeft(0x12, '0').Substring(12, 3)) + "Thousand ";
 50 			}
 51 			else if (parts[0].Length > 12)
 52 			{
 53 				returnValue = (((returnValue + HundredsText(parts[0].PadLeft(15, '0').Substring(0, 3)) +
 54 					"Trillion ") + HundredsText(parts[0].PadLeft(15, '0').Substring(3, 3)) + "Billion ") +
 55 					HundredsText(parts[0].PadLeft(15, '0').Substring(6, 3)) + "Million ") +
 56 					HundredsText(parts[0].PadLeft(15, '0').Substring(9, 3)) + "Thousand ";
 57 			}
 58 			else if (parts[0].Length > 9)
 59 			{
 60 				returnValue = ((returnValue + HundredsText(parts[0].PadLeft(12, '0').Substring(0, 3)) +
 61 					"Billion ") + HundredsText(parts[0].PadLeft(12, '0').Substring(3, 3)) + "Million ") +
 62 					HundredsText(parts[0].PadLeft(12, '0').Substring(6, 3)) + "Thousand ";
 63 			}
 64 			else if (parts[0].Length > 6)
 65 			{
 66 				returnValue = (returnValue + HundredsText(parts[0].PadLeft(9, '0').Substring(0, 3)) +
 67 					"Million ") + HundredsText(parts[0].PadLeft(9, '0').Substring(3, 3)) + "Thousand ";
 68 			}
 69 			else if (parts[0].Length > 3)
 70 			{
 71 				returnValue = returnValue + HundredsText(parts[0].PadLeft(6, '0').Substring(0, 3)) +
 72 					"Thousand ";
 73 			}
 74 			string hundreds = parts[0].PadLeft(3, '0');
 75 			int tempInt = 0;
 76 			hundreds = hundreds.Substring(hundreds.Length - 3, 3);
 77 			if (int.TryParse(hundreds, out tempInt) == true)
 78 			{
 79 				if (int.Parse(hundreds) < 100)
 80 				{
 81 					//returnValue = returnValue + "and ";
 82 				}
 83 				returnValue = returnValue + HundredsText(hundreds) + "Dollar";
 84 				if (int.Parse(hundreds) != 1)
 85 				{
 86 					returnValue = returnValue + "s";
 87 				}
 88 				if (int.Parse(parts[1]) != 0)
 89 				{
 90 					returnValue = returnValue + " and ";
 91 				}
 92 			}
 93 			if ((parts.Length == 2) && (int.Parse(parts[1]) != 0))
 94 			{
 95 				returnValue = returnValue + HundredsText(parts[1].PadLeft(3, '0')) + "Cent";
 96 				if (int.Parse(parts[1]) != 1)
 97 				{
 98 					returnValue = returnValue + "s";
 99 				}
100 			}
101 			return returnValue;
102 		}
103 
104 
105 		static string[] Tens = new string[] { 
106 			"Ten",
107 			"Twenty", 
108 			"Thirty", 
109 			"Forty", 
110 			"Fifty", 
111 			"Sixty", 
112 			"Seventy", 
113 			"Eighty", 
114 			"Ninety" };
115 		static string[] Ones = new string[] { 
116 			"One",
117 			"Two",
118 			"Three",
119 			"Four",
120 			"Five",
121 			"Six",
122 			"Seven",
123 			"Eight",
124 			"Nine",
125 			"Ten",
126 			"Eleven",
127 			"Twelve", 
128 			"Thirteen", 
129 			"Fourteen", 
130 			"Fifteen", 
131 			"Sixteen", 
132 			"Seventeen", 
133 			"Eighteen", 
134 			"Nineteen" };
135 
136 
137 
138 		private static string HundredsText(string value)
139 		{
140 			char Val_1;
141 			char Val_2;
142 
143 			string returnValue = "";
144 			bool IsSingleDigit = true;
145 			char Val = value[0];
146 			if (int.Parse(Val.ToString()) != 0)
147 			{
148 				Val_1 = value[0];
149 				returnValue = returnValue + Ones[int.Parse(Val_1.ToString()) - 1] + " Hundred ";
150 				IsSingleDigit = false;
151 			}
152 			Val_1 = value[1];
153 			if (int.Parse(Val_1.ToString()) > 1)
154 			{
155 				Val = value[1];
156 				returnValue = returnValue + Tens[int.Parse(Val.ToString()) - 1] + " ";
157 				Val_1 = value[2];
158 				if (int.Parse(Val_1.ToString()) != 0)
159 				{
160 					Val = value[2];
161 					returnValue = returnValue + Ones[int.Parse(Val.ToString()) - 1] + " ";
162 				}
163 				return returnValue;
164 			}
165 			Val_1 = value[1];
166 			if (int.Parse(Val_1.ToString()) == 1)
167 			{
168 				Val = value[1];
169 				Val_2 = value[2];
170 				return (returnValue + Ones[int.Parse(Val.ToString() + Val_2.ToString()) - 1] + " ");
171 			}
172 			Val_2 = value[2];
173 			if (int.Parse(Val_2.ToString()) == 0)
174 			{
175 				return returnValue;
176 			}
177 			if (!IsSingleDigit)
178 			{
179 				returnValue = returnValue + "and ";
180 			}
181 			Val_2 = value[2];
182 			return (returnValue + Ones[int.Parse(Val_2.ToString()) - 1] + " ");
183 		}