-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberToWords.ts
94 lines (90 loc) · 2.38 KB
/
numberToWords.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
function recursiveNumToWords(amount: number): string {
const numberInText = [
"",
"satu",
"dua",
"tiga",
"empat",
"lima",
"enam",
"tujuh",
"delapan",
"sembilan",
"sepuluh",
"sebelas",
];
if (amount < 12) {
return numberInText[amount] as string;
}
if (amount < 20) {
return recursiveNumToWords(amount - 10) + " belas";
}
if (amount < 100) {
return (
recursiveNumToWords(Math.floor(amount / 10)) +
" puluh " +
recursiveNumToWords(amount % 10)
);
}
if (amount < 200) {
return "seratus " + recursiveNumToWords(amount - 100);
}
if (amount < 1_000) {
return (
recursiveNumToWords(Math.floor(amount / 100)) +
" ratus " +
recursiveNumToWords(amount % 100)
);
}
if (amount < 2_000) {
return "seribu " + recursiveNumToWords(amount - 1_000);
}
if (amount < 1_000_000) {
return (
recursiveNumToWords(Math.floor(amount / 1_000)) +
" ribu " +
recursiveNumToWords(amount % 1_000)
);
}
if (amount < 1_000_000_000) {
return (
recursiveNumToWords(Math.floor(amount / 1_000_000)) +
" juta " +
recursiveNumToWords(amount % 1_000_000)
);
}
if (amount < 1_000_000_000_000) {
return (
recursiveNumToWords(Math.floor(amount / 1_000_000_000)) +
" milyar " +
recursiveNumToWords(amount % 1_000_000_000)
);
}
if (amount < 1_000_000_000_000) {
return (
recursiveNumToWords(Math.floor(amount / 1_000_000_000_000)) +
" trilyun " +
recursiveNumToWords(amount % 1_000_000_000_000)
);
}
return "";
}
function trimExcessSpaces(input: string): string {
return input.replace(/ +/g, " ").trim();
}
function numberToWords(amount: number): string {
return trimExcessSpaces(recursiveNumToWords(amount));
}
// test
// console.log(numberToWords(200_000));
// console.log(numberToWords(250_000));
// console.log(numberToWords(252_000));
console.log(numberToWords(200_000) === "dua ratus ribu");
console.log(numberToWords(250_000) === "dua ratus lima puluh ribu");
console.log(numberToWords(252_000) === "dua ratus lima puluh dua ribu");
console.log(numberToWords(2_000_000) === "dua juta");
console.log(numberToWords(250_000_001) === "dua ratus lima puluh juta satu");
console.log(
numberToWords(11_987_654) ===
"sebelas juta sembilan ratus delapan puluh tujuh ribu enam ratus lima puluh empat"
);