-
Notifications
You must be signed in to change notification settings - Fork 0
/
num2text.go
230 lines (183 loc) · 3.87 KB
/
num2text.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
/*
* Ali Ahmadi
* Dedicated to MHM
*/
package num2text
import (
"fmt"
"strconv"
"strings"
)
func conversionError(num string) (string, error) {
return "", fmt.Errorf("error in conversion %v", num)
}
func Convert(num string) (string, error) {
num = strings.TrimSpace(num)
var err error
num, err = replaceChars(num,
[]string{"۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"},
[]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
)
if err != nil {
return conversionError(num)
}
ok, err := numberValidator(num)
if err != nil {
return conversionError(err.Error())
}
if !ok {
return "", fmt.Errorf("%v contain invalid characters", num)
}
isn := (num[0] == '-')
grp := strings.Split(num, ".")
var ing string
if grp[0] != "" {
ing = grp[0]
} else {
ing = "0"
}
var dec string
if len(grp) > 1 && grp[1] != "" {
dec = grp[1]
} else {
dec = "0"
}
if isn {
ing = ing[1:]
}
ing = cleanNum(ing, false)
dec = cleanNum(dec, true)
if len(dec) > 12 {
dec = dec[:12]
}
tmpDec, err := strconv.Atoi(dec)
if err != nil {
return conversionError(num)
}
tmpIng, err := strconv.Atoi(ing)
if err != nil {
return conversionError(num)
}
if tmpDec+tmpIng == 0 {
return coll.ones[0], nil
}
res := ""
if isn {
res += coll.n
}
ig := group(ing)
gcount := len(ig)
for i, v := range ig {
v = cleanNum(v, false)
groupt := ""
gcount--
jump := (v == "0")
if tmp, _ := strconv.Atoi(v); tmp >= 100 {
hdigit := cleanNum(strings.Split(strconv.Itoa(tmp/100), ".")[0], false)
hd, _ := strconv.Atoi(hdigit)
groupt += coll.hundreds[hd]
v = fmt.Sprintf("%v", tmp%100)
if v != "0" {
groupt += coll.v
}
}
if tmp, _ := strconv.Atoi(v); tmp >= 20 || tmp == 10 {
tndigit := cleanNum(strings.Split(fmt.Sprintf("%v", tmp/10), ".")[0], false)
td, _ := strconv.Atoi(tndigit)
groupt += coll.tens[td]
v = fmt.Sprintf("%v", tmp%10)
if v != "0" {
groupt += coll.v
}
}
if tmp, _ := strconv.Atoi(v); tmp >= 1 && tmp <= 19 {
groupt += coll.ones[tmp]
}
res += groupt
// TODO - bug from below
if !jump {
res += " " + coll.thousands[gcount]
if gcount != 0 {
tmpgs := make([]string, i)
copy(tmpgs, ig)
mp := makeMap(tmpgs)
for j, g := range tmpgs {
ff, err := strconv.Atoi(g)
if err != nil || ff == 0 {
// tmpgs = append(tmpgs[:j], tmpgs[j+1:]...)
delete(mp, j)
}
}
if len(mp) != 1 {
res += coll.v
}
}
}
// end of bug section
}
if dec != "0" {
if ing == "0" {
res += " "
} else {
res += coll.v
}
decg := group(dec)
dcount := len(decg)
dgc := dcount
for _, gr := range decg {
gr = cleanNum(gr, false)
groupt := ""
dcount--
jump := (gr == "0")
if tmp, _ := strconv.Atoi(gr); tmp >= 100 {
hdigits := cleanNum(strings.Split(strconv.Itoa(tmp/100), ".")[0], false)
ff, _ := strconv.Atoi(hdigits)
groupt += coll.hundreds[ff]
gr = fmt.Sprintf("%v", tmp%100)
if gr != "0" {
groupt += coll.v
}
}
if tmp, _ := strconv.Atoi(gr); tmp >= 20 || tmp == 10 {
tndigits := cleanNum(strings.Split(strconv.Itoa(tmp/10), ".")[0], false)
td, _ := strconv.Atoi(tndigits)
groupt += coll.tens[td]
gr = fmt.Sprintf("%v", tmp%10)
if gr != "0" {
groupt += coll.v
}
}
if tmp, _ := strconv.Atoi(gr); tmp >= 1 && tmp <= 19 {
groupt += coll.ones[tmp]
}
res += groupt
if !jump {
res += " " + coll.thousands[gcount]
if gcount != 0 {
res += coll.v
}
}
}
z := ""
if dgc == 1 {
z = ""
}
dgl := len(decg[0])
if dgl == 1 {
res += coll.tens[1] + z
} else if dgl == 2 {
res += coll.hundreds[1] + z
} else if dgl == 3 {
dgc++
}
res += coll.thousands[dgc-1] + coll.m
}
return cleanStr(res), nil
}
func makeMap(s []string) map[int]string {
mp := map[int]string{}
for i, v := range s {
mp[i] = v
}
return mp
}