Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  
   8  class InterestRate
   9  {
  10      /**
  11       * EFFECT.
  12       *
  13       * Returns the effective interest rate given the nominal rate and the number of
  14       *        compounding payments per year.
  15       *
  16       * Excel Function:
  17       *        EFFECT(nominal_rate,npery)
  18       *
  19       * @param mixed $nominalRate Nominal interest rate as a float
  20       * @param mixed $periodsPerYear Integer number of compounding payments per year
  21       *
  22       * @return float|string
  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 Functions::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 Functions::NAN();
  67          }
  68  
  69          // Calculate
  70          return $periodsPerYear * (($effectiveRate + 1) ** (1 / $periodsPerYear) - 1);
  71      }
  72  }