Browse Source

zhCalc,不使用公式计算,未引入math时不报错

MaiXinRong 6 months ago
parent
commit
cf214f1a02
1 changed files with 13 additions and 4 deletions
  1. 13 4
      app/public/js/zh_calc.js

+ 13 - 4
app/public/js/zh_calc.js

@@ -1,15 +1,14 @@
 'use strict';
 
 /**
- *
+ * 1. 必须引用decimal.min.js
+ * 2. 如果使用ZhCalc.mathCalcExpr和ZhCalc.tranExpr必须引用math.min.js
  * @author Mai
  * @date
  * @version
  */
 
-;math.config({
-    number: 'BigNumber',
-});
+;
 const zhBaseCalc = (function () {
     const zeroPrecision = 12, mulPrecision = 12, divPrecision = 12;
 
@@ -71,6 +70,7 @@ const zhBaseCalc = (function () {
  * @type {{add, sub, mul, div, round}}
  */
 const ZhCalc = (function () {
+    let mathConfig = false;
     Decimal.set({precision: 50, defaults: true});
     const percentReg = /((\d+)|((\d+)(\.\d+)))%/g;
     /**
@@ -286,7 +286,16 @@ const ZhCalc = (function () {
         },
     };
 
+    function checkMathConfig () {
+        if (!mathConfig) {
+            math && math.config({
+                number: 'BigNumber',
+            });
+            mathConfig = true;
+        }
+    }
     function mathCalcExpr(expr) {
+        checkMathConfig();
         return parseFloat(math.evaluate(expr));
     }
     function tansExpr(expr) {