|
@@ -1,37 +1,49 @@
|
|
|
'use strict';
|
|
|
const Decimal = require('decimal.js');
|
|
|
+const _ = require('lodash');
|
|
|
// Decimal.set({ precision: 2 });
|
|
|
module.exports = {
|
|
|
+ turnDecimal(n) {
|
|
|
+ if (_.isObject(n)) {
|
|
|
+ n = JSON.parse(JSON.stringify(n));
|
|
|
+ if (_.isObject(n)) n = _.get(n, '$numberDecimal');
|
|
|
+ }
|
|
|
+ return new Decimal(n);
|
|
|
+ },
|
|
|
// 加法
|
|
|
plus(n1 = 0, n2 = 0) {
|
|
|
- const number1 = new Decimal(n1);
|
|
|
- const number2 = new Decimal(n2);
|
|
|
+ const number1 = this.turnDecimal(n1);
|
|
|
+ const number2 = this.turnDecimal(n2);
|
|
|
const result = number1.add(number2).toFixed(2, Decimal.ROUND_DOWN);
|
|
|
return this.toNumber(result);
|
|
|
},
|
|
|
// 减法
|
|
|
minus(n1 = 0, n2 = 0) {
|
|
|
- const number1 = new Decimal(n1);
|
|
|
- const number2 = new Decimal(n2);
|
|
|
+ const number1 = this.turnDecimal(n1);
|
|
|
+ const number2 = this.turnDecimal(n2);
|
|
|
const result = number1.minus(number2).toFixed(2, Decimal.ROUND_DOWN);
|
|
|
return this.toNumber(result);
|
|
|
},
|
|
|
// 乘法
|
|
|
multiply(n1 = 0, n2 = 0) {
|
|
|
- const number1 = new Decimal(n1);
|
|
|
- const number2 = new Decimal(n2);
|
|
|
+ const number1 = this.turnDecimal(n1);
|
|
|
+ const number2 = this.turnDecimal(n2);
|
|
|
const result = number1.mul(number2).toFixed(2, Decimal.ROUND_DOWN);
|
|
|
return this.toNumber(result);
|
|
|
},
|
|
|
// 除法
|
|
|
divide(n1 = 0, n2 = 0) {
|
|
|
- const number1 = new Decimal(n1);
|
|
|
- const number2 = new Decimal(n2);
|
|
|
+ const number1 = this.turnDecimal(n1);
|
|
|
+ const number2 = this.turnDecimal(n2);
|
|
|
const result = number1.div(number2).toFixed(2, Decimal.ROUND_DOWN);
|
|
|
return this.toNumber(result);
|
|
|
},
|
|
|
|
|
|
toNumber(num) {
|
|
|
+ if (_.isObject(num)) {
|
|
|
+ num = JSON.parse(JSON.stringify(num));
|
|
|
+ if (_.isObject(num)) num = _.get(num, '$numberDecimal');
|
|
|
+ }
|
|
|
return new Decimal(num).toNumber();
|
|
|
},
|
|
|
};
|