JS 将数字串转换成带逗号的显示方式

114 阅读1分钟

将数字转换成带逗号的显示方式,例: 123,456,789

 filters: {
    changeNum(num) {
      if (!num) return num
      // 将数字串转换成带逗号的显示方式
      if (!/^([+\-])?(\d+)(\.\d+)?$/.test(num)) {
        return num
      }
      const a = RegExp.$1
      let b = RegExp.$2
      const c = RegExp.$3
      const re = new RegExp().compile('(\\d)(\\d{3})(,|$)')
      while (re.test(b)) { b = b.replace(re, '$1,$2$3') }
      return a + '' + b + '' + c
    }
    },

在代码中使用。。。。。。。。。。。。。。。。。。

image.png