Browse Source

处理数字千位分隔符

chenrui 3 years ago
parent
commit
30b2c2b458
1 changed files with 8 additions and 0 deletions
  1. 8 0
      src/libs/tools.js

+ 8 - 0
src/libs/tools.js

@@ -225,6 +225,14 @@ export const formatDecimal = function (num, decimal) {
   }
   }
   return parseFloat(num).toFixed(decimal)
   return parseFloat(num).toFixed(decimal)
 }
 }
+// 处理数字千位分隔符
+export const toThousands = (num) => {
+  return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
+    return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
+      return $1 + ','
+    })
+  })
+}
 // 只能输入数字
 // 只能输入数字
 export const justNumber = function (val) {
 export const justNumber = function (val) {
   let _value = val + ''
   let _value = val + ''