diff --git a/CHANGELOG.md b/CHANGELOG.md index ca24d6f0e5..ddeef1773c 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 [#1417](https://github.com/PHPOffice/PhpSpreadsheet/pull/1417) ## [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',