Skip to content

Commit 8818a7d

Browse files
author
MarkBaker
committed
Merge branch 'master' into Issue-2551-Array-Ready-Function-Financial
# Conflicts: # phpstan-baseline.neon # src/PhpSpreadsheet/Calculation/Financial/Dollar.php
2 parents df12b06 + 88da4e1 commit 8818a7d

File tree

5 files changed

+53
-24
lines changed

5 files changed

+53
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4949

5050
### Fixed
5151

52+
- 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)
5253
- 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)
5354
- 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)
5455
- Xlsx Reader merge range fixes.

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -965,21 +965,6 @@ parameters:
965965
count: 1
966966
path: src/PhpSpreadsheet/Calculation/Financial/Depreciation.php
967967

968-
-
969-
message: "#^Cannot cast mixed to int\\.$#"
970-
count: 2
971-
path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php
972-
973-
-
974-
message: "#^Parameter \\#1 \\$number of function floor expects float, mixed given\\.$#"
975-
count: 2
976-
path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php
977-
978-
-
979-
message: "#^Parameter \\#1 \\$x of function fmod expects float, mixed given\\.$#"
980-
count: 2
981-
path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php
982-
983968
-
984969
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:atMaturity\\(\\) should return float\\|string but returns mixed\\.$#"
985970
count: 1

src/PhpSpreadsheet/Calculation/Financial/Dollar.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78
use PhpOffice\PhpSpreadsheet\Calculation\TextData\Format;
89

@@ -50,11 +51,17 @@ public static function decimal($fractionalDollar = null, $fraction = 0)
5051
return self::evaluateArrayArguments([self::class, __FUNCTION__], $fractionalDollar, $fraction);
5152
}
5253

53-
$fractionalDollar = Functions::flattenSingleValue($fractionalDollar);
54-
$fraction = (int) Functions::flattenSingleValue($fraction);
54+
try {
55+
$fractionalDollar = FinancialValidations::validateFloat(
56+
Functions::flattenSingleValue($fractionalDollar) ?? 0.0
57+
);
58+
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
59+
} catch (Exception $e) {
60+
return $e->getMessage();
61+
}
5562

56-
// Validate parameters
57-
if ($fractionalDollar === null || $fraction < 0) {
63+
// Additional parameter validations
64+
if ($fraction < 0) {
5865
return Functions::NAN();
5966
}
6067
if ($fraction == 0) {
@@ -92,18 +99,24 @@ public static function fractional($decimalDollar = null, $fraction = 0)
9299
return self::evaluateArrayArguments([self::class, __FUNCTION__], $decimalDollar, $fraction);
93100
}
94101

95-
$decimalDollar = Functions::flattenSingleValue($decimalDollar);
96-
$fraction = (int) Functions::flattenSingleValue($fraction);
102+
try {
103+
$decimalDollar = FinancialValidations::validateFloat(
104+
Functions::flattenSingleValue($decimalDollar) ?? 0.0
105+
);
106+
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
107+
} catch (Exception $e) {
108+
return $e->getMessage();
109+
}
97110

98-
// Validate parameters
99-
if ($decimalDollar === null || $fraction < 0) {
111+
// Additional parameter validations
112+
if ($fraction < 0) {
100113
return Functions::NAN();
101114
}
102115
if ($fraction == 0) {
103116
return Functions::DIV0();
104117
}
105118

106-
$dollars = floor($decimalDollar);
119+
$dollars = ($decimalDollar < 0.0) ? ceil($decimalDollar) : floor($decimalDollar);
107120
$cents = fmod($decimalDollar, 1);
108121
$cents *= $fraction;
109122
$cents *= 10 ** (-ceil(log10($fraction)));

tests/data/Calculation/Financial/DOLLARDE.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
// fractional_dollar, fraction, result
44

55
return [
6+
[
7+
2.5,
8+
1.6,
9+
4,
10+
],
11+
[
12+
-2.5,
13+
-1.6,
14+
4,
15+
],
616
[
717
1.125,
818
1.02,
@@ -38,6 +48,11 @@
3848
1.1200000000000001,
3949
32,
4050
],
51+
[
52+
'#VALUE!',
53+
'Not A Number',
54+
0,
55+
],
4156
[
4257
'#DIV/0!',
4358
1.2344999999999999,

tests/data/Calculation/Financial/DOLLARFR.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
// decimal_dollar, fraction, result
44

55
return [
6+
[
7+
1.24,
8+
1.6,
9+
4,
10+
],
11+
[
12+
-1.24,
13+
-1.6,
14+
4,
15+
],
616
[
717
1.02,
818
1.125,
@@ -38,6 +48,11 @@
3848
1.375,
3949
32,
4050
],
51+
[
52+
'#VALUE!',
53+
'Not A Number',
54+
0,
55+
],
4156
[
4257
'#DIV/0!',
4358
1.2344999999999999,

0 commit comments

Comments
 (0)