Browse Source

金额转换千分符格式

lxf 2 years ago
parent
commit
51b48b1f18
2 changed files with 35 additions and 10 deletions
  1. 2 4
      src/main.js
  2. 33 6
      src/utils/util.js

+ 2 - 4
src/main.js

@@ -44,10 +44,7 @@ import {
   selectDictLabels
 } from '@/utils/ruoyi'
 
-import {
-  dictDataEcho
-}
-from '@/utils/util'
+import { dictDataEcho, moneyFormat } from '@/utils/util'
 
 // 分页组件
 import Pagination from '@/components/Pagination'
@@ -79,6 +76,7 @@ app.config.globalProperties.selectDictLabel = selectDictLabel
 app.config.globalProperties.selectDictLabels = selectDictLabels
 //字典回显
 app.config.globalProperties.dictDataEcho = dictDataEcho
+app.config.globalProperties.moneyFormat = moneyFormat
 
 
 

+ 33 - 6
src/utils/util.js

@@ -1,12 +1,39 @@
 //根据value值回显字典label值
 export function dictDataEcho(value, arr) {
   if (value && arr) {
-    value = value + ''
-    const current = arr.find(x => x.dictKey === value)
+    value = value + "";
+    const current = arr.find((x) => x.dictKey === value);
     if (current != undefined && current.dictValue) {
-      return current.dictValue
+      return current.dictValue;
     }
-    return ''
+    return "";
   }
-  return ''
-}
+  return "";
+}
+
+// 金额千分符
+export function moneyFormat(s, n) {
+  if (s) {
+    s = s + "";
+    let str = s.slice(0, 1);
+    if (str === "-") {
+      s = s.slice(1, s.length);
+    }
+    n = n > 0 && n <= 20 ? n : 2;
+    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
+    var l = s.split(".")[0].split("").reverse(),
+      r = s.split(".")[1];
+    var t = "";
+    for (let i = 0; i < l.length; i++) {
+      t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? "," : "");
+    }
+    let result = t.split("").reverse().join("") + "." + r;
+    if (str === "-") {
+      return "-" + result;
+    } else {
+      return result;
+    }
+  } else {
+    return "0.00";
+  }
+}