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