From f9f9f4cacfe1398f3ab0f1b709e71bbea044b6d2 Mon Sep 17 00:00:00 2001 From: n-longcape Date: Thu, 12 Mar 2020 12:37:07 +0900 Subject: [PATCH] Fix ROUNDUP and ROUNDDOWN for negative number Closes #1417 --- 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 a25c9c51..1fa12b3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,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 [#1417](https://github.com/PHPOffice/PhpSpreadsheet/pull/1417) - Fix loading styles from vmlDrawings when containing whitespace [#1347](https://github.com/PHPOffice/PhpSpreadsheet/issues/1347) - Fix incorrect behavior when removing last row [#1365](https://github.com/PHPOffice/PhpSpreadsheet/pull/1365) - MATCH with a static array should return the position of the found value based on the values submitted [#1332](https://github.com/PHPOffice/PhpSpreadsheet/pull/1332) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 590ad99b..f94c8fcc 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1139,9 +1139,7 @@ class MathTrig 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); @@ -1167,9 +1165,7 @@ class MathTrig 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 499c5a5b..b1b3ac71 100644 --- a/tests/data/Calculation/MathTrig/ROUNDDOWN.php +++ b/tests/data/Calculation/MathTrig/ROUNDDOWN.php @@ -71,6 +71,16 @@ return [ 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 c1782b2b..f522e83f 100644 --- a/tests/data/Calculation/MathTrig/ROUNDUP.php +++ b/tests/data/Calculation/MathTrig/ROUNDUP.php @@ -66,11 +66,21 @@ return [ 4.4400, 2, ], + [ + -4.44, + -4.4400, + 2, + ], [ 5.20, 2.26 + 2.94, 2, ], + [ + -5.20, + -2.26 - 2.94, + 2, + ], [ '#VALUE!', 'ABC',