货币显示格式化方法
/**
* 货币分隔符
* @param {string} m 需要转换的字符串
* @param {number} p 分隔符位数,默认 3
*/
const money = (m,p=3)=>m.slice(0,m.length%p)+((m.length%p)?',':'')+m.slice(m.length%p).replace(new RegExp(`\\d\{${p}\}`,'g'),(n,i)=>(i?',':'')+n);
示例:
console.log(money('12345678'))
// 12,345,789
console.log(money('12345678', 4))
// 1234,5789