-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
228 lines (220 loc) · 6.25 KB
/
index.ts
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
const thousands = [
"тисяч",
"тисяча",
"тисячі",
"тисячі",
"тисячі",
"тисяч",
"тисяч",
"тисяч",
"тисяч",
"тисяч",
"тисяч",
];
const hundreds = [
"",
"сто",
"двісті",
"триста",
"чотириста",
"п'ятсот",
"шістсот",
"сімсот",
"вісімсот",
"дев'ятсот",
];
const dozens = [
"",
"десять",
"двадцять",
"тридцять",
"сорок",
"п'ятдесят",
"шістдесят",
"сімдесят",
"вісімдесят",
"дев'яносто",
];
const numbers = [
"нуль",
"одна",
"дві",
"три",
"чотири",
"п'ять",
"шість",
"сім",
"вісім",
"дев'ять",
"десять",
];
const dozensBeetween11and19 = [
"",
"одинадцять",
"дванадцять",
"тринадцять",
"чотирнадцять",
"п'ятнадцять",
"шістнадцять",
"сімнадцять",
"вісімнадцять",
"дев'ятнадцять",
"десять",
];
const coins = [
"копійок",
"копійка",
"копійки",
"копійки",
"копійки",
"копійок",
"копійок",
"копійок",
"копійок",
"копійок",
"копійок",
];
const hryvnias = [
"гривень",
"гривня",
"гривні",
"гривні",
"гривні",
"гривень",
"гривень",
"гривень",
"гривень",
"гривень",
"гривень",
];
export default function convertToWords(number: number) {
const numberBeforeDot = number.toString().split(".")[0];
let numberAfterDot = number.toString().split(".")[1];
if (!numberAfterDot) {
numberAfterDot = "00";
}
if (numberAfterDot.length > 2) {
numberAfterDot = numberAfterDot.slice(0, 2);
}
if (numberBeforeDot.length === 5) {
const result: string[] = []
if (checkIfnumberBetwen11and19(numberBeforeDot.slice(0, 2))) {
result.push(dozensBeetween11and19[parseInt(numberBeforeDot.slice(1, 2))]);
} else {
result.push(returnDozens(numberBeforeDot.slice(0, 1)));
if (numberBeforeDot.slice(1, 2) !== "0") {
result.push(returnNumbers(numberBeforeDot.slice(1, 2)));
}
}
result.push(`тисяч ${returHundreds(numberBeforeDot.slice(2, 3))}`);
if (checkIfnumberBetwen11and19(numberBeforeDot.slice(3, 5))) {
result.push(
`${dozensBeetween11and19[parseInt(numberBeforeDot.slice(4, 5))]} гривень`
);
} else {
result.push(`${dozens[parseInt(numberBeforeDot.slice(3, 4))]}`);
if (numberBeforeDot.slice(4, 5) !== "0") {
result.push(returnNumbers(numberBeforeDot.slice(4, 5)));
}
result.push(returnHryvnias(numberBeforeDot.slice(4, 5)));
}
result.push(returnCoinsWithNumber(numberAfterDot));
const resultString = result.join(" ");
return resultString
}
if (numberBeforeDot.length === 4) {
const result: string[] = []
result.push(returnNumbers(numberBeforeDot.slice(0, 1)));
result.push(thousands[parseInt(numberBeforeDot.slice(0, 1))])
result.push(returHundreds(numberBeforeDot.slice(1, 2)));
if (checkIfnumberBetwen11and19(numberBeforeDot.slice(2, 4))) {
result.push(dozensBeetween11and19[parseInt(numberBeforeDot.slice(3, 4))]);
} else {
result.push(dozens[parseInt(numberBeforeDot.slice(2, 3))]);
if (numberBeforeDot.slice(3, 4) !== "0") {
result.push(returnNumbers(numberBeforeDot.slice(3, 4)));
}
}
result.push(hryvnias[parseInt(numberBeforeDot.slice(3, 4))])
result.push(returnCoinsWithNumber(numberAfterDot))
const resultString = result.join(" ");
return resultString;
}
if (numberBeforeDot.length === 3) {
const result: string[] = []
result.push(returHundreds(numberBeforeDot.slice(0, 1)));
if (checkIfnumberBetwen11and19(numberBeforeDot.slice(1, 3))) {
result.push(dozensBeetween11and19[parseInt(numberBeforeDot.slice(2, 3))]);
result.push(hryvnias[0])
} else {
result.push(dozens[parseInt(numberBeforeDot.slice(1, 2))]);
if (numberBeforeDot.slice(2, 3) !== "0") {
result.push(returnNumbers(numberBeforeDot.slice(2, 3)));
}
result.push(returnHryvnias(numberBeforeDot.slice(2, 3)));
}
result.push(returnCoinsWithNumber(numberAfterDot));
const resultString = result.join(" ")
return resultString
}
if (numberBeforeDot.length === 2) {
const result: string[] = []
if (checkIfnumberBetwen11and19(numberBeforeDot)) {
result.push(`${dozensBeetween11and19[parseInt(numberBeforeDot.slice(1, 2))]} гривень`);
} else {
result.push(returnDozens(numberBeforeDot.slice(0, 1)));
if (numberBeforeDot.slice(1, 2) !== "0") {
result.push(returnNumbers(numberBeforeDot.slice(1, 2)));
}
result.push(returnHryvnias(numberBeforeDot.slice(1, 2)));
}
result.push(`${returnCoinsWithNumber(numberAfterDot)}`);
const resultString = result.join(" ")
return resultString;
}
if (numberBeforeDot.length === 1) {
const result: string[] = []
result.push(returnNumbers(numberBeforeDot.slice(0, 1)));
result.push(returnHryvnias(numberBeforeDot.slice(0, 1)));
result.push(returnCoinsWithNumber(numberAfterDot));
const resultString = result.join(" ");
return resultString;
}
return "Помилочка";
}
function checkIfnumberBetwen11and19(string: string) {
const number = parseInt(string);
if (number <= 19 && number >= 11) {
return true;
}
return false;
}
function returHundreds(string: string) {
const number = parseInt(string);
return hundreds[number];
}
function returnCoinsWithNumber(string: string) {
let returnedString = "";
if (string.slice(0, 1) === "0") {
returnedString += `${string.slice(1, 2)}`;
} else {
returnedString += `${string}0`;
}
returnedString += " копійок";
return returnedString;
}
function returnHryvnias(string: string) {
const number = parseInt(string);
return hryvnias[number];
}
function returnDozens(string: string) {
if (string) {
const number = parseInt(string);
return dozens[number];
}
return "";
}
function returnNumbers(string: string) {
const number = parseInt(string);
return numbers[number];
}