-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordify.go
315 lines (290 loc) · 6.73 KB
/
wordify.go
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package wordify
import (
"math"
"strings"
)
type unidad uint8
const (
cero unidad = iota
uno
dos
tres
cuatro
cinco
seis
siete
ocho
nueve
diez
once
doce
trece
catorce
quince
dieciseis
diecisiete
dieciocho
diecinueve
veinte
veintiuno
veintidos
veintitres
veinticuatro
veinticinco
veintiseis
veintisiete
veintiocho
veintinueve
)
func (d unidad) String() string {
return [30]string{
"cero",
"uno",
"dos",
"tres",
"cuatro",
"cinco",
"seis",
"siete",
"ocho",
"nueve",
"diez",
"once",
"doce",
"trece",
"catorce",
"quince",
"dieciséis",
"diecisiete",
"dieciocho",
"diecinueve",
"veinte",
"veintiuno",
"veintidós",
"veintitrés",
"veinticuatro",
"veinticinco",
"veintiséis",
"veintisiete",
"veintiocho",
"veintinueve",
}[d]
}
type decena uint8
const (
treinta decena = 10 * (iota + 3)
cuarenta
cincuenta
sesenta
setenta
ochenta
noventa
)
func (d decena) String() string {
toIdx := map[decena]uint8{
treinta: 0,
cuarenta: 1,
cincuenta: 2,
sesenta: 3,
setenta: 4,
ochenta: 5,
noventa: 6,
}
return [7]string{
"treinta",
"cuarenta",
"cincuenta",
"sesenta",
"setenta",
"ochenta",
"noventa",
}[toIdx[d]]
}
type centena uint16
const (
cien centena = 100 * (iota + 1)
doscientos
trescientos
cuatrocientos
quinientos
seiscientos
setecientos
ochocientos
novecientos
)
func (c centena) String() string {
toIdx := map[centena]uint8{
cien: 0,
doscientos: 1,
trescientos: 2,
cuatrocientos: 3,
quinientos: 4,
seiscientos: 5,
setecientos: 6,
ochocientos: 7,
novecientos: 8,
}
return [9]string{
"ciento",
"doscientos",
"trescientos",
"cuatrocientos",
"quinientos",
"seiscientos",
"setecientos",
"ochocientos",
"novecientos",
}[toIdx[c]]
}
// oom: [o]rders [o]f [m]agnitud
type oom uint8
const (
cientos oom = iota + 1
miles
millones
milesDeMillones
)
// useful keywords to build the number word
const (
and string = " y "
space string = " "
un string = "un"
menos string = "menos"
hundred string = "cien"
twentyone string = "veintiún"
thousand string = "mil"
million string = "millón"
millions string = "millones"
)
// Int returns the Spanish word representation of a given integer number
// ranging from -999_999_999_999 up to 999_999_999_999
func Int(num int) string {
var (
number int
spanishWord string
)
if num < 0 {
number = -1 * num
spanishWord += menos + space
} else {
number = num
}
numLen := numLenght(number)
// numbers are grouped into powers of ten according to:
// 10^(3p)
// with p in {0, 1, 2, ..., n} so that,
// Group 1: 10^(3*0) <= numbers < 10^(3*1)
// Group 2: 10^(3*1) <= numbers < 10^(3*2)
// Group N: 10^(3*(N-1)) <= numbers < 10^(3*N)
oomGroups := make(map[oom]int)
for i := 1; i <= numLen/3; i++ {
// algorithm to store numbers according to their corresponding group:
// (number % 10^(3*i)) / 10^(3*(i-1))
oomGroups[oom(i)] = (number % int(math.Pow10(3*i))) / int(math.Pow10(3*(i-1)))
}
// if the lenght of the number is not divisible by 3, then we need to manually
// add the left most numbers that will be stored into the highest ranked group,
// that is, the group with the highest upper limit. For this we used the algorithm:
// number / 10^(number length - remainder)
remainder := numLen % 3
if remainder != 0 {
idx := 1 + numLen/3
oomGroups[oom(idx)] = number / int(math.Pow10(numLen-remainder))
}
cientosRes := ""
milesRes := ""
millonesRes := ""
milesDeMillonesRes := ""
for group, numbers := range oomGroups {
switch group {
case cientos:
cientosRes = numberToWords(numbers, "")
case miles:
milesRes = numberToWords(numbers, thousand)
case millones:
millonesRes = numberToWords(numbers, millions)
case milesDeMillones:
milesDeMillonesRes = numberToWords(numbers, thousand)
}
}
spanishWord += strings.TrimSpace(milesDeMillonesRes + space + millonesRes + space + milesRes + space + cientosRes)
return spanishWord
}
func numberToWords(number int, orderOfMag string) string {
uni := extractorUnidad(number)
dec := extractorDecena(number)
cen := extractorCentena(number)
isUn := ""
numberIsOne := ""
numberIsTwentyOne := ""
switch orderOfMag {
case millions:
isUn = un
numberIsOne = un + space + million
numberIsTwentyOne = twentyone
case "":
isUn = unidad(1).String()
numberIsOne = unidad(1).String()
numberIsTwentyOne = unidad(21).String()
default:
isUn = un
numberIsOne = orderOfMag
numberIsTwentyOne = twentyone
}
numberInWords := ""
if number == 0 {
numberInWords = ""
} else if number == 1 {
numberInWords = numberIsOne
} else if number < 30 && number%100 == 21 {
numberInWords = numberIsTwentyOne + space + orderOfMag
} else if number < 30 {
numberInWords = unidad(number).String() + space + orderOfMag
} else if number == 100 {
numberInWords = hundred + space + orderOfMag
} else if number < 100 && number >= 30 && uni == 0 {
numberInWords = decena(dec).String() + space + orderOfMag
} else if number < 100 && number >= 30 && uni == 1 {
numberInWords = decena(dec).String() + and + isUn + space + orderOfMag
} else if number < 100 && number >= 30 {
numberInWords = decena(dec).String() + and + unidad(uni).String() + space + orderOfMag
} else if number >= 100 && number%100 == 0 {
numberInWords = centena(cen).String() + space + orderOfMag
} else if number >= 100 && number%100 == 1 {
numberInWords = centena(cen).String() + space + isUn + space + orderOfMag
} else if number >= 100 && number%100 == 21 {
numberInWords = centena(cen).String() + space + numberIsTwentyOne + space + orderOfMag
} else if number >= 100 && number%100 < 30 {
numberInWords = centena(cen).String() + space + unidad(number%100).String() + space + orderOfMag
} else if number >= 100 && number%100 >= 30 && uni == 0 {
numberInWords = centena(cen).String() + space + decena(dec).String() + space + orderOfMag
} else if number >= 100 && number%100 >= 30 && uni == 1 {
numberInWords = centena(cen).String() + space + decena(dec).String() + and + isUn + space + orderOfMag
} else {
numberInWords = centena(cen).String() + space + decena(dec).String() + and + unidad(uni).String() + space + orderOfMag
}
return numberInWords
}
func numLenght(num int) int {
count := 0
for num > 0 {
num = num / 10
count++
}
return count
}
// extractorCentena returns the number in the hundreds place.
// Eg: 122 -> 100
func extractorCentena(num int) int {
return 100 * (num / 100)
}
// extractorDecena returns the number in the tenths place.
// Eg: 122 -> 20
func extractorDecena(num int) int {
return 10 * ((num % 100) / 10)
}
// extractorUnidad returns the number in the units place.
// Eg: 122 -> 2
func extractorUnidad(num int) int {
return num % 10
}