JS数字格式化为千分位

醉菜鸟 2022-05-18 08:00:11

/** 
 * js 千分符 
 * @param num 
 * @returns {string} 
 */
function groupSeparator(num) { 
    num = num + '';   
    if(!num.includes('.')){     
        num += '.'   
    }   
    return num.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {  
        return $1 + ',';    
    }).replace(/\.$/, '');
}
意见反馈