-
Notifications
You must be signed in to change notification settings - Fork 954
/
DutchNumberToWordsConverter.cs
186 lines (167 loc) · 4.59 KB
/
DutchNumberToWordsConverter.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
namespace Humanizer;
/// <summary>
/// Dutch spelling of numbers is not really officially regulated.
/// There are a few different rules that can be applied.
/// Used the rules as stated here.
/// http://www.beterspellen.nl/website/?pag=110
/// </summary>
class DutchNumberToWordsConverter :
GenderlessNumberToWordsConverter
{
static readonly string[] UnitsMap = ["nul", "een", "twee", "drie", "vier", "vijf", "zes", "zeven", "acht", "negen", "tien", "elf", "twaalf", "dertien", "veertien", "vijftien", "zestien", "zeventien", "achttien", "negentien"];
static readonly string[] TensMap = ["nul", "tien", "twintig", "dertig", "veertig", "vijftig", "zestig", "zeventig", "tachtig", "negentig"];
class Fact
{
public long Value { get; set; }
public required string Name { get; set; }
public required string Prefix { get; set; }
public required string Postfix { get; set; }
public bool DisplayOneUnit { get; set; }
}
static readonly Fact[] Hunderds =
[
new()
{
Value = 1_000_000_000_000_000_000L,
Name = "triljoen",
Prefix = " ",
Postfix = " ",
DisplayOneUnit = true
},
new()
{
Value = 1_000_000_000_000_000L,
Name = "biljard",
Prefix = " ",
Postfix = " ",
DisplayOneUnit = true
},
new()
{
Value = 1_000_000_000_000L,
Name = "biljoen",
Prefix = " ",
Postfix = " ",
DisplayOneUnit = true
},
new()
{
Value = 1000000000,
Name = "miljard",
Prefix = " ",
Postfix = " ",
DisplayOneUnit = true
},
new()
{
Value = 1000000,
Name = "miljoen",
Prefix = " ",
Postfix = " ",
DisplayOneUnit = true
},
new()
{
Value = 1000,
Name = "duizend",
Prefix = "",
Postfix = " ",
DisplayOneUnit = false
},
new()
{
Value = 100,
Name = "honderd",
Prefix = "",
Postfix = "",
DisplayOneUnit = false
}
];
public override string Convert(long input)
{
var number = input;
if (number == 0)
{
return UnitsMap[0];
}
if (number < 0)
{
return $"min {Convert(-number)}";
}
var word = "";
foreach (var m in Hunderds)
{
var divided = number / m.Value;
if (divided <= 0)
{
continue;
}
if (divided == 1 && !m.DisplayOneUnit)
{
word += m.Name;
}
else
{
word += Convert(divided) + m.Prefix + m.Name;
}
number %= m.Value;
if (number > 0)
{
word += m.Postfix;
}
}
if (number > 0)
{
if (number < 20)
{
word += UnitsMap[number];
}
else
{
var tens = TensMap[number / 10];
var unit = number % 10;
if (unit > 0)
{
var units = UnitsMap[unit];
var trema = units.EndsWith("e");
word += units + (trema ? "ën" : "en") + tens;
}
else
{
word += tens;
}
}
}
return word;
}
static readonly Dictionary<string, string> OrdinalExceptions = new()
{
{
"een", "eerste"
},
{
"drie", "derde"
},
{
"miljoen", "miljoenste"
},
};
static readonly char[] EndingCharForSte = ['t', 'g', 'd'];
public override string ConvertToOrdinal(int number)
{
var word = Convert(number);
foreach (var kv in OrdinalExceptions.Where(kv => word.EndsWith(kv.Key)))
{
// replace word with exception
return StringHumanizeExtensions.Concat(word.AsSpan(0, word.Length - kv.Key.Length), kv.Value.AsSpan());
}
// achtste
// twintigste, dertigste, veertigste, ...
// honderdste, duizendste, ...
if (word.LastIndexOfAny(EndingCharForSte) == word.Length - 1)
{
return word + "ste";
}
return word + "de";
}
}