From 07b58022d4b4930a72b4f347f54a4f006f72c76c Mon Sep 17 00:00:00 2001 From: n-longcape Date: Thu, 12 Mar 2020 12:37:07 +0900 Subject: [PATCH 1/2] Fix ROUNDUP and ROUNDDOWN for negative number --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/MathTrig.php | 8 ++------ tests/data/Calculation/MathTrig/ROUNDDOWN.php | 10 ++++++++++ tests/data/Calculation/MathTrig/ROUNDUP.php | 10 ++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca24d6f0e5..db00e96657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed - Fix ROUNDUP and ROUNDDOWN for floating-point rounding error [#1404](https://github.com/PHPOffice/PhpSpreadsheet/pull/1404) +- Fix ROUNDUP and ROUNDDOWN for negative number ## [1.11.0] - 2020-03-02 diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 73403686b2..196f497d46 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1065,9 +1065,7 @@ public static function ROUNDUP($number, $digits) if ((is_numeric($number)) && (is_numeric($digits))) { if ($number < 0.0) { - $significance = pow(10, (int) $digits); - - return floor($number * $significance) / $significance; + return round($number - 0.5 * pow(0.1, $digits), $digits, PHP_ROUND_HALF_DOWN); } return round($number + 0.5 * pow(0.1, $digits), $digits, PHP_ROUND_HALF_DOWN); @@ -1093,9 +1091,7 @@ public static function ROUNDDOWN($number, $digits) if ((is_numeric($number)) && (is_numeric($digits))) { if ($number < 0.0) { - $significance = pow(10, (int) $digits); - - return ceil($number * $significance) / $significance; + return round($number + 0.5 * pow(0.1, $digits), $digits, PHP_ROUND_HALF_UP); } return round($number - 0.5 * pow(0.1, $digits), $digits, PHP_ROUND_HALF_UP); diff --git a/tests/data/Calculation/MathTrig/ROUNDDOWN.php b/tests/data/Calculation/MathTrig/ROUNDDOWN.php index 499c5a5bdb..b1b3ac71f8 100644 --- a/tests/data/Calculation/MathTrig/ROUNDDOWN.php +++ b/tests/data/Calculation/MathTrig/ROUNDDOWN.php @@ -71,6 +71,16 @@ 2.26 + 2.94, 2, ], + [ + -4.44, + -4.4400, + 2, + ], + [ + -5.20, + -2.26 - 2.94, + 2, + ], [ '#VALUE!', 'ABC', diff --git a/tests/data/Calculation/MathTrig/ROUNDUP.php b/tests/data/Calculation/MathTrig/ROUNDUP.php index c1782b2b07..f522e83f6f 100644 --- a/tests/data/Calculation/MathTrig/ROUNDUP.php +++ b/tests/data/Calculation/MathTrig/ROUNDUP.php @@ -66,11 +66,21 @@ 4.4400, 2, ], + [ + -4.44, + -4.4400, + 2, + ], [ 5.20, 2.26 + 2.94, 2, ], + [ + -5.20, + -2.26 - 2.94, + 2, + ], [ '#VALUE!', 'ABC', From 180d1b94b568fec4183a83f2906d78bd099df92e Mon Sep 17 00:00:00 2001 From: n-longcape Date: Thu, 12 Mar 2020 14:03:58 +0900 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db00e96657..ddeef1773c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed - Fix ROUNDUP and ROUNDDOWN for floating-point rounding error [#1404](https://github.com/PHPOffice/PhpSpreadsheet/pull/1404) -- Fix ROUNDUP and ROUNDDOWN for negative number +- Fix ROUNDUP and ROUNDDOWN for negative number [#1417](https://github.com/PHPOffice/PhpSpreadsheet/pull/1417) ## [1.11.0] - 2020-03-02