js-(19,999,999.00)

function price(n, precision) {
let s = String(n), int = parseInt(n).toString(), pre = s.split('.')[1];
let start = int.length % 3;

let r = s.substr(0, start)

for (let i = start; i < int.length; i += 3) {
r += (r.length ? ',' : '') + int.substr(i, 3)
}

if (pre) {
r += '.' + `${pre}000000`.substr(0, precision || 2)
} else if (precision) {
r += '.' + '000000'.substr(0, precision || 2)
}

return r
}
console.log(price(19999999.00,2))

posted @ 2018-07-10 16:49  年少的你如此美丽  阅读(213)  评论(0编辑  收藏  举报