From b6ff857ba794311069a34694fbd49ed2b3b30d84 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Mon, 22 Jul 2024 08:32:54 -0700 Subject: [PATCH] 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. --- .../CashFlow/Constant/Periodic/Interest.php | 4 ++-- tests/data/Calculation/Financial/RATE.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Interest.php b/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Interest.php index 833c5734e9..ad68ec133b 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Interest.php +++ b/src/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Interest.php @@ -165,7 +165,7 @@ public static function rate( $guess = ($guess === null) ? 0.1 : Functions::flattenSingleValue($guess); try { - $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods); + $numberOfPeriods = CashFlowValidations::validateFloat($numberOfPeriods); $payment = CashFlowValidations::validateFloat($payment); $presentValue = CashFlowValidations::validatePresentValue($presentValue); $futureValue = CashFlowValidations::validateFutureValue($futureValue); @@ -193,7 +193,7 @@ public static function rate( return $close ? $rate : ExcelError::NAN(); } - private static function rateNextGuess(float $rate, int $numberOfPeriods, float $payment, float $presentValue, float $futureValue, int $type): string|float + private static function rateNextGuess(float $rate, float $numberOfPeriods, float $payment, float $presentValue, float $futureValue, int $type): string|float { if ($rate == 0.0) { return ExcelError::NAN(); diff --git a/tests/data/Calculation/Financial/RATE.php b/tests/data/Calculation/Financial/RATE.php index f5a5ab4920..bea4ce9a1c 100644 --- a/tests/data/Calculation/Financial/RATE.php +++ b/tests/data/Calculation/Financial/RATE.php @@ -29,6 +29,18 @@ -250, 5000, ], + [ + 0.017929869399484, + 24.99, + -250, + 5000, + ], + [ + 0.016581855, + 24.5, + -250, + 5000, + ], [ 0.016550119066711999, 24,