-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
136 lines (127 loc) · 4.54 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
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 - present MH Rohman Masyhar
*/
class Terbilang {
private words = [
'',
'Satu',
'Dua',
'Tiga',
'Empat',
'Lima',
'Enam',
'Tujuh',
'Delapan',
'Sembilan',
'Sepuluh',
'Sebelas'
]
public Convert (convertNumber: number) {
const value = Math.abs(Number(convertNumber))
let word = ''
/* 1 - 12 */
if (value < 12) {
word = this.words[value]
} else if (value < 20) {
/* 12 - 19 */
word = this.words[value - 10] + ' Belas'
} else if (value < 100) {
/* 20 - 99 */
const primary = value / 10
const start = parseInt(String(primary).substr(0, 1), 10)
const end = value % 10
word = this.words[start] + ' Puluh ' + this.words[end]
} else if (value < 200) {
/* 100 - 199 */
word = 'Seratus ' + this.Convert(value - 100)
} else if (value < 1000) {
/* 200 - 999 */
const primary = value / 100
const start = parseInt(String(primary).substr(0, 1), 10)
const end = value % 100
word = this.words[start] + ' Ratus ' + this.Convert(end)
} else if (value < 2000) {
/* 1,000 - 1,999 */
word = 'Seribu ' + this.Convert(value - 1000)
} else if (value < 10000) {
/* 2,000 - 9,999 */
const primary = value / 1000
const start = parseInt(String(primary).substr(0, 1), 10)
const end = value % 1000
word = this.words[start] + ' Ribu ' + this.Convert(end)
} else if (value < 100000) {
/* 10,000 - 99,999 */
const primary = value / 100
const start = parseInt(String(primary).substr(0, 2), 10)
const end = value % 1000
word = this.Convert(start) + ' Ribu ' + this.Convert(end)
} else if (value < 1000000) {
/* 100,000 - 999,999 */
const primary = value / 1000
const start = parseInt(String(primary).substr(0, 3), 10)
const end = value % 1000
word = this.Convert(start) + ' Ribu ' + this.Convert(end)
} else if (value < 100000000) {
/* 1,000,000 - 99,999,999 */
const primary = value / 1000000
const start = parseInt(String(primary).substr(0, 4), 10)
const end = value % 1000000
word = this.Convert(start) + ' Juta ' + this.Convert(end)
} else if (value < 1000000000) {
const primary = value / 1000000
const start = parseInt(String(primary).substr(0, 4), 10)
const end = value % 1000000
word = this.Convert(start) + ' Juta ' + this.Convert(end)
} else if (value < 10000000000) {
const primary = value / 1000000000
const start = parseInt(String(primary).substr(0, 1), 10)
const end = value % 1000000000
word = this.Convert(start) + ' Milyar ' + this.Convert(end)
} else if (value < 100000000000) {
const primary = value / 1000000000
const start = parseInt(String(primary).substr(0, 2), 10)
const end = value % 1000000000
word = this.Convert(start) + ' Milyar ' + this.Convert(end)
} else if (value < 1000000000000) {
const primary = value / 1000000000
const start = parseInt(String(primary).substr(0, 3), 10)
const end = value % 1000000000
word = this.Convert(start) + ' Milyar ' + this.Convert(end)
} else if (value < 10000000000000) {
const primary = value / 10000000000
const start = parseInt(String(primary).substr(0, 1), 10)
const end = value % 10000000000
word = this.Convert(start) + ' Triliun ' + this.Convert(end)
} else if (value < 100000000000000) {
const primary = value / 1000000000000
const start = parseInt(String(primary).substr(0, 2), 10)
const end = value % 1000000000000
word = this.Convert(start) + ' Triliun ' + this.Convert(end)
} else if (value < 1000000000000000) {
const primary = value / 1000000000000
const start = parseInt(String(primary).substr(0, 3), 10)
const end = value % 1000000000000
word = this.Convert(start) + ' Triliun ' + this.Convert(end)
} else if (value < 10000000000000000) {
const primary = value / 1000000000000000
const start = parseInt(String(primary).substr(0, 1), 10)
const end = value % 1000000000000000
word = this.Convert(start) + ' Kuadriliun ' + this.Convert(end)
}
const split = word.split(' ')
const full = []
for (const wordSplit of split) {
if (wordSplit !== '') {
full.push(wordSplit)
}
}
return full.join(' ')
}
}
function convertTerbilang (value: number) {
const terbilang = new Terbilang()
return terbilang.Convert(value)
}
export default convertTerbilang