Skip to content

Commit

Permalink
Fix bug #1626 where values of 0 were "rounded" up/down as if they wer…
Browse files Browse the repository at this point in the history
…e not 0 (#1627)

* Fix bug where values of 0 were "rounded" up/down as if they were not 0
  • Loading branch information
Flinsch authored Dec 10, 2020
1 parent 9289ab1 commit 1f2f2c7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- PrintArea causes exception [#1544](https://github.com/phpoffice/phpspreadsheet/pull/1544)
- PrintArea causes exception [#1544](https://github.com/phpoffice/phpspreadsheet/pull/1544)
- ROUNDUP and ROUNDDOWN return incorrect results for values of 0 [#1627](https://github.com/phpoffice/phpspreadsheet/pull/1627)
- Calculation/DateTime Failure With PHP8 [#1661](https://github.com/phpoffice/phpspreadsheet/pull/1661)
- Reader/Gnumeric Failure with PHP8 [#1662](https://github.com/phpoffice/phpspreadsheet/pull/1662)
- ReverseSort bug, exposed but not caused by PHP8 [#1660](https://github.com/phpoffice/phpspreadsheet/pull/1660)
Expand Down
8 changes: 8 additions & 0 deletions src/PhpSpreadsheet/Calculation/MathTrig.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,10 @@ public static function ROUNDUP($number, $digits)
$digits = Functions::flattenSingleValue($digits);

if ((is_numeric($number)) && (is_numeric($digits))) {
if ($number == 0.0) {
return 0.0;
}

if ($number < 0.0) {
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
}
Expand All @@ -1134,6 +1138,10 @@ public static function ROUNDDOWN($number, $digits)
$digits = Functions::flattenSingleValue($digits);

if ((is_numeric($number)) && (is_numeric($digits))) {
if ($number == 0.0) {
return 0.0;
}

if ($number < 0.0) {
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/data/Calculation/MathTrig/ROUNDDOWN.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

return [
[
0,
0,
2,
],
[
662,
662.78999999999996,
Expand Down
5 changes: 5 additions & 0 deletions tests/data/Calculation/MathTrig/ROUNDUP.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

return [
[
0,
0,
2,
],
[
663,
662.78999999999996,
Expand Down

0 comments on commit 1f2f2c7

Please sign in to comment.