乐趣区

关于前端:js-数字隔3位加逗号

function formatNumber(val){if (val === undefined){return '暂无数据'} else if (isNaN(+val)){return val} else if (!val){return 0}

    let newVal = val.toString()
    let arr = newVal.split('.')
    let inpart = arr[0].replace(/(\d)(?=(?:\d{3})+$)/g,'$1,')
    if (arr[1]){return inpart + '.' + arr[1]
    }else {return inpart}
}

console.log(formatNumber(12345678));

后果:

退出移动版