number format

by lemon kazi

JavaScript

console.log(format(57899));
function format(num, fix=3) {
        //num = parseFloat(num.replace(/,/g, ''));
        //return new Intl.NumberFormat('ja-JP', { maximumSignificantDigits: fix }).format(num)
         var p = num.toFixed(2).split(".");
         return p[0].split("").reduceRight(function(acc, num, i, orig) {
             if ("-" === num && 0 === i) {
                 return num + acc;
             }
             var pos = orig.length - i - 1
             return  num + (pos && !(pos % 3) ? "," : "") + acc;
         }, ""); 
				 //+ (p[1] ? "." + p[1] : "");
    }