From bc7e0f7035dbbdefaf2b225c9f0b01e4ee8d6a62 Mon Sep 17 00:00:00 2001 From: ccrlawrence Date: Mon, 8 Aug 2022 18:07:24 +0100 Subject: [PATCH] Math.round bug fix (2077.14 * 1000 + 5.59 * 1000)/1000 = 2082.7299999999996 Math.round to round before division. --- lib/operators/numeric.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/operators/numeric.js b/lib/operators/numeric.js index 7bcb402..a510657 100644 --- a/lib/operators/numeric.js +++ b/lib/operators/numeric.js @@ -32,7 +32,7 @@ exports.numericAdd = function(oLeft, oRight) { var nLeft = oLeft.valueOf(), nRight = oRight.valueOf(), nPower = cMath.pow(10, fOperator_numeric_getPower(nLeft, nRight)); - return fOperator_numeric_getResultOfType(oLeft, oRight, ((nLeft * nPower) + (nRight * nPower))/nPower); + return fOperator_numeric_getResultOfType(oLeft, oRight, Math.round((nLeft * nPower) + (nRight * nPower))/nPower); }; // op:numeric-subtract($arg1 as numeric, $arg2 as numeric) as numeric @@ -40,7 +40,7 @@ exports.numericSubtract = function(oLeft, oRight) { var nLeft = oLeft.valueOf(), nRight = oRight.valueOf(), nPower = cMath.pow(10, fOperator_numeric_getPower(nLeft, nRight)); - return fOperator_numeric_getResultOfType(oLeft, oRight, ((nLeft * nPower) - (nRight * nPower))/nPower); + return fOperator_numeric_getResultOfType(oLeft, oRight, Math.round((nLeft * nPower) - (nRight * nPower))/nPower); }; // op:numeric-multiply($arg1 as numeric, $arg2 as numeric) as numeric @@ -48,7 +48,7 @@ exports.numericMultiply = function(oLeft, oRight) { var nLeft = oLeft.valueOf(), nRight = oRight.valueOf(), nPower = cMath.pow(10, fOperator_numeric_getPower(nLeft, nRight)); - return fOperator_numeric_getResultOfType(oLeft, oRight, ((nLeft * nPower) * (nRight * nPower))/(nPower * nPower)); + return fOperator_numeric_getResultOfType(oLeft, oRight, Math.round((nLeft * nPower) * (nRight * nPower))/(nPower * nPower)); }; // op:numeric-divide($arg1 as numeric, $arg2 as numeric) as numeric @@ -117,4 +117,4 @@ function fOperator_numeric_getResultOfType(oLeft, oRight, nResult) { return new (oLeft instanceof cXSInteger && oRight instanceof cXSInteger && nResult % 1 ? cXSInteger : cXSDecimal)(nResult); }; -module.exports = exports; \ No newline at end of file +module.exports = exports;