数字格式化10000 => 10,000
2020-02-26 本文已影响0人
简单背包客
/**
* 数字格式化
* 10000 => "10,000"
* @param num 需要格式化的数字
*/
function toThousandFilter(num) {
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
}
//用法:
toThousandFilter(12345678); // 12,345,678