forked from rolling-scopes-school/human-readable-number
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
86 lines (68 loc) · 2.05 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function toReadable(number) {
const obj = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten',
11: 'eleven',
12: 'twelve',
13: 'thirteen',
14: 'fourteen',
15: 'fifteen',
16: 'sixteen',
17: 'seventeen',
18: 'eighteen',
19: 'nineteen',
20: 'twenty',
30: 'thirty',
40: 'forty',
50: 'fifty',
60: 'sixty',
70: 'seventy',
80: 'eighty',
90: 'ninety',
100: 'hundred',
};
let num = '';
if ((number) / 100 > 1) {
num += obj[Math.trunc(number / 100)] + ' ' + obj[100];
number = number - Math.trunc(number / 100) * 100;
}
if (number > 0 && number < 20) {
num += ' ' + obj[number];
return num;
}
if ((number) / 10 > 1) {
num += ' ' + obj[Math.trunc(number / 10) * 10];
number = number - Math.trunc(number / 10) * 10;
}
if (number > 0) {
num += ' ' + obj[number];
}
if (num === '' && number === 0) {
return obj[0]
};
return num.trim();
}
console.log(toReadable(811));
//num = 900;
// console.log(num % 100);
</script>
</body>
</html>