| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class InterestRate |
| 10 |
{ |
| 11 |
/** |
| 12 |
* EFFECT. |
| 13 |
* |
| 14 |
* Returns the effective interest rate given the nominal rate and the number of |
| 15 |
* compounding payments per year. |
| 16 |
* |
| 17 |
* Excel Function: |
| 18 |
* EFFECT(nominal_rate,npery) |
| 19 |
* |
| 20 |
* @param mixed $nominalRate Nominal interest rate as a float |
| 21 |
* @param mixed $periodsPerYear Integer number of compounding payments per year |
| 22 |
* @return string|float |
| 23 |
*/ |
| 24 |
public static function effective($nominalRate = 0, $periodsPerYear = 0) |
| 25 |
{ |
| 26 |
$nominalRate = Functions::flattenSingleValue($nominalRate); |
| 27 |
$periodsPerYear = Functions::flattenSingleValue($periodsPerYear); |
| 28 |
|
| 29 |
try { |
| 30 |
$nominalRate = FinancialValidations::validateFloat($nominalRate); |
| 31 |
$periodsPerYear = FinancialValidations::validateInt($periodsPerYear); |
| 32 |
} catch (Exception $e) { |
| 33 |
return $e->getMessage(); |
| 34 |
} |
| 35 |
|
| 36 |
if ($nominalRate <= 0 || $periodsPerYear < 1) { |
| 37 |
return ExcelError::NAN(); |
| 38 |
} |
| 39 |
|
| 40 |
return ((1 + $nominalRate / $periodsPerYear) ** $periodsPerYear) - 1; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* NOMINAL. |
| 45 |
* |
| 46 |
* Returns the nominal interest rate given the effective rate and the number of compounding payments per year. |
| 47 |
* |
| 48 |
* @param mixed $effectiveRate Effective interest rate as a float |
| 49 |
* @param mixed $periodsPerYear Integer number of compounding payments per year |
| 50 |
* |
| 51 |
* @return float|string Result, or a string containing an error |
| 52 |
*/ |
| 53 |
public static function nominal($effectiveRate = 0, $periodsPerYear = 0) |
| 54 |
{ |
| 55 |
$effectiveRate = Functions::flattenSingleValue($effectiveRate); |
| 56 |
$periodsPerYear = Functions::flattenSingleValue($periodsPerYear); |
| 57 |
|
| 58 |
try { |
| 59 |
$effectiveRate = FinancialValidations::validateFloat($effectiveRate); |
| 60 |
$periodsPerYear = FinancialValidations::validateInt($periodsPerYear); |
| 61 |
} catch (Exception $e) { |
| 62 |
return $e->getMessage(); |
| 63 |
} |
| 64 |
|
| 65 |
if ($effectiveRate <= 0 || $periodsPerYear < 1) { |
| 66 |
return ExcelError::NAN(); |
| 67 |
} |
| 68 |
|
| 69 |
// Calculate |
| 70 |
return $periodsPerYear * (($effectiveRate + 1) ** (1 / $periodsPerYear) - 1); |
| 71 |
} |
| 72 |
} |
| 73 |
|