Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

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