Skip to content

Fix for DOLLARDE() and DOLLARFR() with negative dollar values #2579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Fix bug with `DOLLARDE()` and `DOLLARFR()` functions when the dollar value is negative [Issue #2578](https://github.com/PHPOffice/PhpSpreadsheet/issues/2578) [PR #2579](https://github.com/PHPOffice/PhpSpreadsheet/pull/2579)
- Fix partial function name matching when translating formulae from Russian to English [Issue #2533](https://github.com/PHPOffice/PhpSpreadsheet/issues/2533) [PR #2534](https://github.com/PHPOffice/PhpSpreadsheet/pull/2534)
- Various bugs related to Conditional Formatting Rules, and errors in the Xlsx Writer for Conditional Formatting [PR #2491](https://github.com/PHPOffice/PhpSpreadsheet/pull/2491)
- Xlsx Reader merge range fixes.
Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -960,21 +960,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Depreciation.php

-
message: "#^Cannot cast mixed to int\\.$#"
count: 2
path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php

-
message: "#^Parameter \\#1 \\$number of function floor expects float, mixed given\\.$#"
count: 2
path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php

-
message: "#^Parameter \\#1 \\$x of function fmod expects float, mixed given\\.$#"
count: 2
path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:atMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#"
count: 1
Expand Down
33 changes: 23 additions & 10 deletions src/PhpSpreadsheet/Calculation/Financial/Dollar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\TextData\Format;

Expand Down Expand Up @@ -40,18 +41,24 @@ public static function format($number, $precision = 2): string
*/
public static function decimal($fractionalDollar = null, $fraction = 0)
{
$fractionalDollar = Functions::flattenSingleValue($fractionalDollar);
$fraction = (int) Functions::flattenSingleValue($fraction);
try {
$fractionalDollar = FinancialValidations::validateFloat(
Functions::flattenSingleValue($fractionalDollar) ?? 0.0
);
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
} catch (Exception $e) {
return $e->getMessage();
}

// Validate parameters
if ($fractionalDollar === null || $fraction < 0) {
// Additional parameter validations
if ($fraction < 0) {
return Functions::NAN();
}
if ($fraction == 0) {
return Functions::DIV0();
}

$dollars = floor($fractionalDollar);
$dollars = ($fractionalDollar < 0.0) ? ceil($fractionalDollar) : floor($fractionalDollar);
$cents = fmod($fractionalDollar, 1);
$cents /= $fraction;
$cents *= 10 ** ceil(log10($fraction));
Expand All @@ -76,18 +83,24 @@ public static function decimal($fractionalDollar = null, $fraction = 0)
*/
public static function fractional($decimalDollar = null, $fraction = 0)
{
$decimalDollar = Functions::flattenSingleValue($decimalDollar);
$fraction = (int) Functions::flattenSingleValue($fraction);
try {
$decimalDollar = FinancialValidations::validateFloat(
Functions::flattenSingleValue($decimalDollar) ?? 0.0
);
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
} catch (Exception $e) {
return $e->getMessage();
}

// Validate parameters
if ($decimalDollar === null || $fraction < 0) {
// Additional parameter validations
if ($fraction < 0) {
return Functions::NAN();
}
if ($fraction == 0) {
return Functions::DIV0();
}

$dollars = floor($decimalDollar);
$dollars = ($decimalDollar < 0.0) ? ceil($decimalDollar) : floor($decimalDollar);
$cents = fmod($decimalDollar, 1);
$cents *= $fraction;
$cents *= 10 ** (-ceil(log10($fraction)));
Expand Down
15 changes: 15 additions & 0 deletions tests/data/Calculation/Financial/DOLLARDE.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
// fractional_dollar, fraction, result

return [
[
2.5,
1.6,
4,
],
[
-2.5,
-1.6,
4,
],
[
1.125,
1.02,
Expand Down Expand Up @@ -38,6 +48,11 @@
1.1200000000000001,
32,
],
[
'#VALUE!',
'Not A Number',
0,
],
[
'#DIV/0!',
1.2344999999999999,
Expand Down
15 changes: 15 additions & 0 deletions tests/data/Calculation/Financial/DOLLARFR.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
// decimal_dollar, fraction, result

return [
[
1.24,
1.6,
4,
],
[
-1.24,
-1.6,
4,
],
[
1.02,
1.125,
Expand Down Expand Up @@ -38,6 +48,11 @@
1.375,
32,
],
[
'#VALUE!',
'Not A Number',
0,
],
[
'#DIV/0!',
1.2344999999999999,
Expand Down