This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numbertoltstr.js
77 lines (67 loc) · 2.27 KB
/
numbertoltstr.js
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
let _numbers = {0: "nulis", 1: "vienas", 2: "du", 3: "trys", 4: "keturi", 5: "penki",
6: "šeši", 7: "septyni", 8: "aštuoni", 9: "devyni", 10: "dešimt",
11: "vienuolika", 12: "dvylika", 13:"trylika", 14: "keturiolika",
15: "penkiolika", 16: "šešiolika", 17: "septyniolika",
18: "aštuoniolika", 19: "devyniolika"
};
let _tens = {2: 'dvidešimt', 3: 'trisdešimt', 4: 'keturiasdešimt', 5: 'penkiasdešimt',
6: 'šešiasdešimt', 7: 'septyniasdešimt', 8: 'aštuoniasdešimt', 9: 'devyniasdešimt'
};
let _forms = {
'eur': ["euras", "eurai", "eurų"],
'10x2': ["šimtas", "šimtai"], // third form not available as never used
'10x3': ["tūkstantis", "tūkstančiai", "tūkstančių"],
'10x6': ["milijonas", "milijonai", "milijonų"],
'10x9': ["milijardas", "milijardai", "milijardų"],
'10x12': ["trilijonas", "trilijonai", "trilijonų"],
'10x15': ["kvadrilijonas", "kvadrilijonai", "kvadrilijonų"]
};
function getForm (number) {
if ((number >= 10 && number <= 19) || number % 10 == 0) {
return 2;
}
if (number % 10 === 1) {
return 0;
}
else {
return 1;
}
}
function getNumberAsStr (number) {
if (number < 20)
return _numbers[number];
let high = Math.trunc(number / 10) % 10;
let low = number % 10;
let tens = _tens[high];
if (low != 0)
return tens + ' ' + _numbers[low];
return tens;
}
function getNumberToStrHundredsPart (number) {
let hundreds = Math.trunc(number / 100);
let remaining = number % 100;
let hundredsStr = '';
if (hundreds > 0) {
hundredsStr = (hundreds === 1 ? '' : getNumberAsStr(hundreds) + ' ') + _forms['10x2'][getForm(hundreds)];
if (remaining == 0) {
return hundredsStr;
}
return hundredsStr + ' ' + getNumberAsStr(remaining);
}
return getNumberAsStr(remaining);
}
module.exports = function (number) {
let result = '';
let cents = number * 100 % 100;
let intNumber = Math.trunc (number);
let iter = 0;
do {
let numberStr = getNumberToStrHundredsPart (intNumber % 1000);
numberStr += ' ' + _forms[iter === 0 ? 'eur' : '10x' + iter*3][getForm (intNumber % 100)];
result = numberStr + (result.length > 0 ? ' ' : '') + result;
iter += 1;
intNumber = Math.trunc (intNumber / 1000);
}
while (intNumber > 0);
return (result + ' ' + cents + ' ct');
}