-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (37 loc) · 1.33 KB
/
index.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
exports.currency_rupiah = (angka, prefix) => {
try {
const text_string = angka.toString().replace(/[^-,\d]/g, "");
const split =
text_string.split("").includes("-") === false
? text_string.split(",")
: text_string.split("-");
split[0] === "" ? (split[0] = "-") : split;
// set minus and comma currency
if (split[0] === "-") {
const comma = split[1].split(",");
const sisa = comma[0].length % 3;
let rupiah = comma[0].substring(0, sisa);
const ribuan = comma[0].substring(sisa).match(/\d{3}/g);
if (ribuan !== null) {
const dot = sisa ? "." : "";
rupiah += dot + ribuan.join(".");
}
rupiah = comma[1] === undefined ? rupiah : rupiah + "," + comma[1];
return prefix === undefined
? split[0] + rupiah
: prefix + split[0] + rupiah;
} else {
const sisa = split[0].length % 3;
let rupiah = split[0].substring(0, sisa);
const ribuan = split[0].substring(sisa).match(/\d{3}/g);
if (ribuan !== null) {
const dot = sisa ? "." : "";
rupiah += dot + ribuan.join(".");
}
rupiah = split[1] === undefined ? rupiah : rupiah + "," + split[1];
return prefix === undefined ? rupiah : prefix + rupiah;
}
} catch (error) {
console.log("regex_convert_mata_uang.ts", error);
}
};