Skip to content

Commit b6ff857

Browse files
committed
RATE Function Permits Floating Point NPER
Suggested by PR #848 from @markkimsal. The RATE calculation had already been corrected, so that part of the PR was unnecessary, however one of the tests included a floating point value for Number of Periods, which Excel permits. PhpSpreadsheet till now expected that parameter to be an integer. This is trivially changed with some tests added.
1 parent 1b68270 commit b6ff857

File tree

2 files changed

+14
-2
lines changed
  • src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic
  • tests/data/Calculation/Financial

2 files changed

+14
-2
lines changed

src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Interest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static function rate(
165165
$guess = ($guess === null) ? 0.1 : Functions::flattenSingleValue($guess);
166166

167167
try {
168-
$numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
168+
$numberOfPeriods = CashFlowValidations::validateFloat($numberOfPeriods);
169169
$payment = CashFlowValidations::validateFloat($payment);
170170
$presentValue = CashFlowValidations::validatePresentValue($presentValue);
171171
$futureValue = CashFlowValidations::validateFutureValue($futureValue);
@@ -193,7 +193,7 @@ public static function rate(
193193
return $close ? $rate : ExcelError::NAN();
194194
}
195195

196-
private static function rateNextGuess(float $rate, int $numberOfPeriods, float $payment, float $presentValue, float $futureValue, int $type): string|float
196+
private static function rateNextGuess(float $rate, float $numberOfPeriods, float $payment, float $presentValue, float $futureValue, int $type): string|float
197197
{
198198
if ($rate == 0.0) {
199199
return ExcelError::NAN();

tests/data/Calculation/Financial/RATE.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
-250,
3030
5000,
3131
],
32+
[
33+
0.017929869399484,
34+
24.99,
35+
-250,
36+
5000,
37+
],
38+
[
39+
0.016581855,
40+
24.5,
41+
-250,
42+
5000,
43+
],
3244
[
3345
0.016550119066711999,
3446
24,

0 commit comments

Comments
 (0)