一些项目在开发过程中,会遇到给表格、图表中的数字添加千分符的问题,下面分享一个简单实用的函数给大家。
const thousands = (str) => { //str 字符类型的数字 let thousandsReg = /(d)(?=(d{3})+$)/g; const numArr = str.split("."); numArr[0] = numArr[0].replace(thousandsReg, "$1,"); return numArr.join(".");};如有不对,欢迎指出。