Skip to content

Commit

Permalink
Fix ROUNDUP and ROUNDDOWN for negative number
Browse files Browse the repository at this point in the history
Closes #1417
  • Loading branch information
n-longcape authored and PowerKiKi committed Apr 27, 2020
1 parent e2f87e8 commit f9f9f4c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions src/PhpSpreadsheet/Calculation/MathTrig.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,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);
Expand All @@ -1167,9 +1165,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);
Expand Down
10 changes: 10 additions & 0 deletions tests/data/Calculation/MathTrig/ROUNDDOWN.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@
2.26 + 2.94,
2,
],
[
-4.44,
-4.4400,
2,
],
[
-5.20,
-2.26 - 2.94,
2,
],
[
'#VALUE!',
'ABC',
Expand Down
10 changes: 10 additions & 0 deletions tests/data/Calculation/MathTrig/ROUNDUP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit f9f9f4c

Please sign in to comment.