diff --git a/CHANGELOG.md b/CHANGELOG.md index 16313206..211d9443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Fix active cell when freeze pane is used [#1323](https://github.com/PHPOffice/PhpSpreadsheet/pull/1323) - Fix XLSX file loading with autofilter containing '$' [#1326](https://github.com/PHPOffice/PhpSpreadsheet/pull/1326) - PHPDoc - Use `@return $this` for fluent methods [#1362](https://github.com/PHPOffice/PhpSpreadsheet/pull/1362) +- Fix ROUNDUP and ROUNDDOWN for floating-point rounding error [#1404](https://github.com/PHPOffice/PhpSpreadsheet/pull/1404) ## [1.10.1] - 2019-12-02 diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 51f3b899..73403686 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1064,12 +1064,13 @@ class MathTrig $digits = Functions::flattenSingleValue($digits); if ((is_numeric($number)) && (is_numeric($digits))) { - $significance = pow(10, (int) $digits); if ($number < 0.0) { + $significance = pow(10, (int) $digits); + return floor($number * $significance) / $significance; } - return ceil($number * $significance) / $significance; + return round($number + 0.5 * pow(0.1, $digits), $digits, PHP_ROUND_HALF_DOWN); } return Functions::VALUE(); @@ -1091,12 +1092,13 @@ class MathTrig $digits = Functions::flattenSingleValue($digits); if ((is_numeric($number)) && (is_numeric($digits))) { - $significance = pow(10, (int) $digits); if ($number < 0.0) { + $significance = pow(10, (int) $digits); + return ceil($number * $significance) / $significance; } - return floor($number * $significance) / $significance; + return round($number - 0.5 * pow(0.1, $digits), $digits, PHP_ROUND_HALF_UP); } return Functions::VALUE(); diff --git a/tests/data/Calculation/MathTrig/ROUNDDOWN.php b/tests/data/Calculation/MathTrig/ROUNDDOWN.php index e8bb7c32..499c5a5b 100644 --- a/tests/data/Calculation/MathTrig/ROUNDDOWN.php +++ b/tests/data/Calculation/MathTrig/ROUNDDOWN.php @@ -61,6 +61,16 @@ return [ 31415.92654, -1, ], + [ + 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 caa7ce37..c1782b2b 100644 --- a/tests/data/Calculation/MathTrig/ROUNDUP.php +++ b/tests/data/Calculation/MathTrig/ROUNDUP.php @@ -61,6 +61,16 @@ return [ 31415.92654, -1, ], + [ + 4.44, + 4.4400, + 2, + ], + [ + 5.20, + 2.26 + 2.94, + 2, + ], [ '#VALUE!', 'ABC',