-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Extensions-NumericEnglishWords.cs
292 lines (275 loc) · 10.4 KB
/
Extensions-NumericEnglishWords.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Towel;
namespace Towel_Testing
{
// [TestClass]
public partial class Extensions_Testing
{
internal (decimal Value, string Words)[] ExplicitTestCases =
{
(42m, "Forty-Two"),
( 0m, "Zero"),
( 1m, "One"),
( 2m, "Two"),
( 3m, "Three"),
( 4m, "Four"),
( 5m, "Five"),
( 6m, "Six"),
( 7m, "Seven"),
( 8m, "Eight"),
( 9m, "Nine"),
( 10m, "Ten"),
( 11m, "Eleven"),
( 12m, "Twelve"),
( 13m, "Thirteen"),
( 14m, "Fourteen"),
( 15m, "Fifteen"),
( 16m, "Sixteen"),
( 17m, "Seventeen"),
( 18m, "Eighteen"),
( 19m, "Nineteen"),
( 20m, "Twenty"),
( 21m, "Twenty-One"),
( 30m, "Thirty"),
( 31m, "Thirty-One"),
( 40m, "Forty"),
( 41m, "Forty-One"),
( 50m, "Fifty"),
( 51m, "Fifty-One"),
( 60m, "Sixty"),
( 61m, "Sixty-One"),
( 70m, "Seventy"),
( 71m, "Seventy-One"),
( 80m, "Eighty"),
( 81m, "Eighty-One"),
( 90m, "Ninety"),
( 91m, "Ninety-One"),
( 100m, "One Hundred"),
( 101m, "One Hundred One"),
( 110m, "One Hundred Ten"),
( 111m, "One Hundred Eleven"),
( 121m, "One Hundred Twenty-One"),
( 200m, "Two Hundred"),
( 201m, "Two Hundred One"),
( 210m, "Two Hundred Ten"),
( 211m, "Two Hundred Eleven"),
( 221m, "Two Hundred Twenty-One"),
( 1000m, "One Thousand"),
( 1001m, "One Thousand One"),
( 1011m, "One Thousand Eleven"),
( 1100m, "One Thousand One Hundred"),
( 1101m, "One Thousand One Hundred One"),
( 1111m, "One Thousand One Hundred Eleven"),
( 10000m, "Ten Thousand"),
( 10001m, "Ten Thousand One"),
( 10010m, "Ten Thousand Ten"),
( 10011m, "Ten Thousand Eleven"),
( 10100m, "Ten Thousand One Hundred"),
( 10101m, "Ten Thousand One Hundred One"),
(100000m, "One Hundred Thousand"),
(100001m, "One Hundred Thousand One"),
( 1000000m, "One Million"),
( 1000001m, "One Million One"),
( 1000000000m, "One Billion"),
( 1000000001m, "One Billion One"),
( 1000000000000m, "One Trillion"),
( 1000000000001m, "One Trillion One"),
( 1000000000000000m, "One Quadrillion"),
( 1000000000000001m, "One Quadrillion One"),
( 1000000000000000000m, "One Quintillion"),
( 1000000000000000001m, "One Quintillion One"),
( 1000000000000000000000m, "One Sextillion"),
( 1000000000000000000001m, "One Sextillion One"),
( 1000000000000000000000000m, "One Septillion"),
( 1000000000000000000000001m, "One Septillion One"),
(1000000000000000000000000000m, "One Octillion"),
(1000000000000000000000000001m, "One Octillion One"),
( -1m, "Negative One"),
( -2m, "Negative Two"),
( -10m, "Negative Ten"),
( -11m, "Negative Eleven"),
( -100m, "Negative One Hundred"),
( -101m, "Negative One Hundred One"),
( -111m, "Negative One Hundred Eleven"),
( -1000m, "Negative One Thousand"),
( -1001m, "Negative One Thousand One"),
( -1010m, "Negative One Thousand Ten"),
( -1011m, "Negative One Thousand Eleven"),
( -1100m, "Negative One Thousand One Hundred"),
( -1101m, "Negative One Thousand One Hundred One"),
( -1111m, "Negative One Thousand One Hundred Eleven"),
(0.1m, "One Tenths"),
(0.01m, "One Hundredths"),
(0.001m, "One Thousandths"),
(0.0001m, "One Ten-Thousandths"),
(0.00001m, "One Hundred-Thousandths"),
(0.000001m, "One Millionths"),
(0.0000001m, "One Ten-Millionths"),
(0.00000001m, "One Hundred-Millionths"),
(0.000000001m, "One Billionths"),
(0.0000000001m, "One Ten-Billionths"),
(0.00000000001m, "One Hundred-Billionths"),
(0.000000000001m, "One Trillionths"),
(0.0000000000001m, "One Ten-Trillionths"),
(0.00000000000001m, "One Hundred-Trillionths"),
(0.000000000000001m, "One Quadrillionths"),
(0.0000000000000001m, "One Ten-Quadrillionths"),
(0.00000000000000001m, "One Hundred-Quadrillionths"),
(0.000000000000000001m, "One Quintillionths"),
(0.0000000000000000001m, "One Ten-Quintillionths"),
(0.00000000000000000001m, "One Hundred-Quintillionths"),
(0.000000000000000000001m, "One Sextillionths"),
(0.0000000000000000000001m, "One Ten-Sextillionths"),
(0.00000000000000000000001m, "One Hundred-Sextillionths"),
(0.000000000000000000000001m, "One Septillionths"),
(0.0000000000000000000000001m, "One Ten-Septillionths"),
(0.00000000000000000000000001m, "One Hundred-Septillionths"),
(0.000000000000000000000000001m, "One Octillionths"),
(0.0000000000000000000000000001m, "One Ten-Octillionths"),
(-0.1m, "Negative One Tenths"),
(-0.01m, "Negative One Hundredths"),
(-0.001m, "Negative One Thousandths"),
(-0.0001m, "Negative One Ten-Thousandths"),
(-0.00001m, "Negative One Hundred-Thousandths"),
(-0.000001m, "Negative One Millionths"),
(-0.0000001m, "Negative One Ten-Millionths"),
(-0.00000001m, "Negative One Hundred-Millionths"),
(-0.000000001m, "Negative One Billionths"),
(-0.0000000001m, "Negative One Ten-Billionths"),
(-0.00000000001m, "Negative One Hundred-Billionths"),
(-0.000000000001m, "Negative One Trillionths"),
(-0.0000000000001m, "Negative One Ten-Trillionths"),
(-0.00000000000001m, "Negative One Hundred-Trillionths"),
(-0.000000000000001m, "Negative One Quadrillionths"),
(-0.0000000000000001m, "Negative One Ten-Quadrillionths"),
(-0.00000000000000001m, "Negative One Hundred-Quadrillionths"),
(-0.000000000000000001m, "Negative One Quintillionths"),
(-0.0000000000000000001m, "Negative One Ten-Quintillionths"),
(-0.00000000000000000001m, "Negative One Hundred-Quintillionths"),
(-0.000000000000000000001m, "Negative One Sextillionths"),
(-0.0000000000000000000001m, "Negative One Ten-Sextillionths"),
(-0.00000000000000000000001m, "Negative One Hundred-Sextillionths"),
(-0.000000000000000000000001m, "Negative One Septillionths"),
(-0.0000000000000000000000001m, "Negative One Ten-Septillionths"),
(-0.00000000000000000000000001m, "Negative One Hundred-Septillionths"),
(-0.000000000000000000000000001m, "Negative One Octillionths"),
(-0.0000000000000000000000000001m, "Negative One Ten-Octillionths"),
(-0.1234m, "Negative One Thousand Two Hundred Thirty-Four Ten-Thousandths"),
( 1.1m, "One And One Tenths"),
( 1.2m, "One And Two Tenths"),
( 10.1m, "Ten And One Tenths"),
( 10.2m, "Ten And Two Tenths"),
( 20.1m, "Twenty And One Tenths"),
( 20.2m, "Twenty And Two Tenths"),
( 1.01m, "One And One Hundredths"),
( 1.02m, "One And Two Hundredths"),
( 2.01m, "Two And One Hundredths"),
( 10.01m, "Ten And One Hundredths"),
( 10.02m, "Ten And Two Hundredths"),
( 20.01m, "Twenty And One Hundredths"),
( 20.02m, "Twenty And Two Hundredths"),
( 101.101m, "One Hundred One And One Hundred One Thousandths"),
(1001.1001m, "One Thousand One And One Thousand One Ten-Thousandths"),
(-0.0100m, "Negative One Hundredths"),
(-0.0000m, "Zero"),
(decimal.MinValue, "Negative Seventy-Nine Octillion Two Hundred Twenty-Eight Septillion One Hundred Sixty-Two Sextillion Five Hundred Fourteen Quintillion Two Hundred Sixty-Four Quadrillion Three Hundred Thirty-Seven Trillion Five Hundred Ninety-Three Billion Five Hundred Forty-Three Million Nine Hundred Fifty Thousand Three Hundred Thirty-Five"),
(decimal.MaxValue, "Seventy-Nine Octillion Two Hundred Twenty-Eight Septillion One Hundred Sixty-Two Sextillion Five Hundred Fourteen Quintillion Two Hundred Sixty-Four Quadrillion Three Hundred Thirty-Seven Trillion Five Hundred Ninety-Three Billion Five Hundred Forty-Three Million Nine Hundred Fifty Thousand Three Hundred Thirty-Five"),
};
[TestMethod]
public void ToEnglishWords_Test()
{
foreach (var (value, words) in ExplicitTestCases)
{
try
{
string towords = value.ToEnglishWords();
Assert.IsTrue(towords == words, $"Expected[{words}] Actual[{towords}]");
}
catch
{
string towords = value.ToEnglishWords();
Assert.IsTrue(towords == words, $"Expected[{words}] Actual[{towords}]");
}
}
}
[TestMethod]
public void TryParseEnglishWords_Test()
{
foreach (var (value, words) in ExplicitTestCases)
{
try
{
var result = Extensions.TryParseEnglishWordsToDecimal(words);
Assert.IsTrue(result == (true, value), $"Expected[{(true, value)}] Actual[{result}]");
}
catch
{
var result = Extensions.TryParseEnglishWordsToDecimal(words);
Assert.IsTrue(result == (true, value), $"Expected[{(true, value)}] Actual[{result}]");
}
}
string?[] shouldFailCases =
{
null,
"",
" One",
"One ",
"One Hundred",
"OneHundred",
"One One",
"Two Two",
"One Hundred One One",
"One One Hundred One",
"One Tenths One",
"TwentyOne",
};
foreach (string? @case in shouldFailCases)
{
try
{
var result = Extensions.TryParseEnglishWordsToDecimal(@case);
Assert.IsTrue(result == (false, default(decimal)), $"Expected[{(false, default(decimal))}] Actual[{result}]");
}
catch
{
var result = Extensions.TryParseEnglishWordsToDecimal(@case);
Assert.IsTrue(result == (false, default(decimal)), $"Expected[{(false, default(decimal))}] Actual[{result}]");
}
}
}
[TestMethod]
public void EnglishWordsSynchronize_Test()
{
for (decimal i = 0; i < 1000000m; i += 13m)
{
try
{
string toEnglishWords = i.ToEnglishWords();
var result = Extensions.TryParseEnglishWordsToDecimal(toEnglishWords);
Assert.IsTrue(result == (true, i), i.ToString());
}
catch
{
string toEnglishWords = i.ToEnglishWords();
var result = Extensions.TryParseEnglishWordsToDecimal(toEnglishWords);
Assert.IsTrue(result == (true, i), i.ToString());
}
}
for (decimal i = -10000m; i < 10000m; i += 1.0001m)
{
try
{
string toEnglishWords = i.ToEnglishWords();
var result = Extensions.TryParseEnglishWordsToDecimal(toEnglishWords);
Assert.IsTrue(result == (true, i), i.ToString());
}
catch
{
string toEnglishWords = i.ToEnglishWords();
var result = Extensions.TryParseEnglishWordsToDecimal(toEnglishWords);
Assert.IsTrue(result == (true, i), i.ToString());
}
}
}
}
}