-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinteger-to-english-words.js
83 lines (76 loc) · 1.81 KB
/
integer-to-english-words.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
78
79
80
81
82
83
/**
* @param {number} num
* @return {string}
*/
var numberToWords = function (num) {
var dict = {};
dict[0] = "Zero";
dict[1] = "One";
dict[2] = "Two";
dict[3] = "Three";
dict[4] = "Four";
dict[5] = "Five";
dict[6] = "Six";
dict[7] = "Seven";
dict[8] = "Eight";
dict[9] = "Nine";
dict[10] = "Ten";
dict[11] = "Eleven";
dict[12] = "Twelve";
dict[13] = "Thirteen";
dict[14] = "Fourteen";
dict[15] = "Fifteen";
dict[16] = "Sixteen";
dict[17] = "Seventeen";
dict[18] = "Eighteen";
dict[19] = "Nineteen";
dict[20] = "Twenty";
dict[30] = "Thirty";
dict[40] = "Forty";
dict[50] = "Fifty";
dict[60] = "Sixty";
dict[70] = "Seventy";
dict[80] = "Eighty";
dict[90] = "Ninety";
dict[100] = "Hundred";
var dict2 = ["Thousand", "Million", "Billion"];
if (num === 0) {
return dict[0];
}
var res = "";
var c = -1;
while (num > 0) {
hundreds(num % 1000);
num = parseInt(num / 1000);
c++;
}
return res.trim();
function hundreds(n) {
var str = "", count = 0;
// 100 - 999
if (n >= 100) {
count = parseInt(n / 100);
n = n % 100;
str += dict[count] + " " + dict[100];
}
// 1-99
if (n !== 0) {
if (str) {
str += " ";
}
// 1-20
if (n <= 20) {
str += dict[n];
} else {
// 21 - 99
var unitdigit = n % 10;
n = n - unitdigit;
str += unitdigit === 0 ? dict[n] : dict[n] + " " + dict[unitdigit];
}
}
if (c >= 0 && str !== "") {
str += " " + dict2[c] + " ";
}
res = (str + res).trim();
}
};