const thousands = (num, decimalNum = 0) => {
let tempArr = [];
const res = [];
tempArr = num.toFixed(decimalNum).toString().split(".");
tempArr[0]
.split("")
.reverse()
.map((item, i) => {
if (i % 3 == 0 && i != 0) {
res.push(",");
}
res.push(item);
});
const decimalPointBefore = res.reverse().join("");
const decimalPointAfter = tempArr.length > 1 ? `.${tempArr[1]}` : "";
return decimalPointBefore + decimalPointAfter;
};
console.log(thousands(123456.546457));
console.log(thousands(123456.546457, 2));