Browse Source

合同支付,公式计算,使用大数运算

MaiXinRong 3 years ago
parent
commit
e7ec22df3d
2 changed files with 15 additions and 17 deletions
  1. 15 2
      app/lib/pay_calc.js
  2. 0 15
      dev4qadx.js

+ 15 - 2
app/lib/pay_calc.js

@@ -9,6 +9,9 @@
  */
 
 const math = require('mathjs');
+math.config({
+    number: 'BigNumber',
+});
 const PayConst = require('../const/deal_pay.js');
 const payType = PayConst.payType;
 const deadlineType = PayConst.deadlineType;
@@ -72,7 +75,12 @@ class PayCalculate {
             }
         }
         try {
-            const value = math.eval(formula);
+            // 使用mathjs计算 math.eval('17259401.95*0.9') = 15533461.754999999,正确应为 15533461.755
+            // const value = math.eval(formula);
+            // 使用逆波兰法四则运算,可防止出现误差,但是只支持四则运算,不支持科学运算
+            // const value = this.ctx.helper.calcExprStrRpn(formula);
+            // 使用mathjs的大数运算,可支持所有
+            const value = parseFloat(math.eval(formula));
             return value;
         } catch(err) {
             return 0;
@@ -92,7 +100,12 @@ class PayCalculate {
             }
         }
         try {
-            const value = math.eval(formula);
+            // 使用mathjs计算 math.eval('17259401.95*0.9') = 15533461.754999999,正确应为 15533461.755
+            // const value = math.eval(formula);
+            // 使用逆波兰法四则运算,可防止出现误差,但是只支持四则运算,不支持科学运算
+            // const value = this.ctx.helper.calcExprStrRpn(formula);
+            // 使用mathjs的大数运算,可支持所有
+            const value = parseFloat(math.eval(formula));
             return value;
         } catch(err) {
             return 0;

+ 0 - 15
dev4qadx.js

@@ -1,15 +0,0 @@
-'use strict';
-
-/**
- *
- *
- * @author Mai
- * @date
- * @version
- */
-
-const fs = require('fs');
-const packageJSON = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8'));
-packageJSON.name = 'calc_dx' ;
-packageJSON.scripts['start-qadx'] = "egg-scripts start --daemon --port 7006";
-fs.writeFileSync(__dirname + '/package.json', JSON.stringify(packageJSON, '', '\t'));