Fix ROUNDUP and ROUNDDOWN for floating-point rounding error (#1404)

Closes #1404
This commit is contained in:
youkan 2020-03-07 14:48:54 +09:00 committed by GitHub
parent a08415a7b5
commit a79b344d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 4 deletions

View File

@ -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 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) - 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) - 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 ## [1.10.1] - 2019-12-02

View File

@ -1064,12 +1064,13 @@ class MathTrig
$digits = Functions::flattenSingleValue($digits); $digits = Functions::flattenSingleValue($digits);
if ((is_numeric($number)) && (is_numeric($digits))) { if ((is_numeric($number)) && (is_numeric($digits))) {
$significance = pow(10, (int) $digits);
if ($number < 0.0) { if ($number < 0.0) {
$significance = pow(10, (int) $digits);
return floor($number * $significance) / $significance; 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(); return Functions::VALUE();
@ -1091,12 +1092,13 @@ class MathTrig
$digits = Functions::flattenSingleValue($digits); $digits = Functions::flattenSingleValue($digits);
if ((is_numeric($number)) && (is_numeric($digits))) { if ((is_numeric($number)) && (is_numeric($digits))) {
$significance = pow(10, (int) $digits);
if ($number < 0.0) { if ($number < 0.0) {
$significance = pow(10, (int) $digits);
return ceil($number * $significance) / $significance; 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(); return Functions::VALUE();

View File

@ -61,6 +61,16 @@ return [
31415.92654, 31415.92654,
-1, -1,
], ],
[
4.44,
4.4400,
2,
],
[
5.20,
2.26 + 2.94,
2,
],
[ [
'#VALUE!', '#VALUE!',
'ABC', 'ABC',

View File

@ -61,6 +61,16 @@ return [
31415.92654, 31415.92654,
-1, -1,
], ],
[
4.44,
4.4400,
2,
],
[
5.20,
2.26 + 2.94,
2,
],
[ [
'#VALUE!', '#VALUE!',
'ABC', 'ABC',