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\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  
   8  class Ceiling
   9  {
  10      /**
  11       * CEILING.
  12       *
  13       * Returns number rounded up, away from zero, to the nearest multiple of significance.
  14       *        For example, if you want to avoid using pennies in your prices and your product is
  15       *        priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the
  16       *        nearest nickel.
  17       *
  18       * Excel Function:
  19       *        CEILING(number[,significance])
  20       *
  21       * @param float $number the number you want the ceiling
  22       * @param float $significance the multiple to which you want to round
  23       *
  24       * @return float|string Rounded Number, or a string containing an error
  25       */
  26      public static function ceiling($number, $significance = null)
  27      {
  28          if ($significance === null) {
  29              self::floorCheck1Arg();
  30          }
  31  
  32          try {
  33              $number = Helpers::validateNumericNullBool($number);
  34              $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
  35          } catch (Exception $e) {
  36              return $e->getMessage();
  37          }
  38  
  39          return self::argumentsOk((float) $number, (float) $significance);
  40      }
  41  
  42      /**
  43       * CEILING.MATH.
  44       *
  45       * Round a number down to the nearest integer or to the nearest multiple of significance.
  46       *
  47       * Excel Function:
  48       *        CEILING.MATH(number[,significance[,mode]])
  49       *
  50       * @param mixed $number Number to round
  51       * @param mixed $significance Significance
  52       * @param int $mode direction to round negative numbers
  53       *
  54       * @return float|string Rounded Number, or a string containing an error
  55       */
  56      public static function math($number, $significance = null, $mode = 0)
  57      {
  58          try {
  59              $number = Helpers::validateNumericNullBool($number);
  60              $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
  61              $mode = Helpers::validateNumericNullSubstitution($mode, null);
  62          } catch (Exception $e) {
  63              return $e->getMessage();
  64          }
  65  
  66          if (empty($significance * $number)) {
  67              return 0.0;
  68          }
  69          if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) {
  70              return floor($number / $significance) * $significance;
  71          }
  72  
  73          return ceil($number / $significance) * $significance;
  74      }
  75  
  76      /**
  77       * CEILING.PRECISE.
  78       *
  79       * Rounds number up, away from zero, to the nearest multiple of significance.
  80       *
  81       * Excel Function:
  82       *        CEILING.PRECISE(number[,significance])
  83       *
  84       * @param mixed $number the number you want to round
  85       * @param float $significance the multiple to which you want to round
  86       *
  87       * @return float|string Rounded Number, or a string containing an error
  88       */
  89      public static function precise($number, $significance = 1)
  90      {
  91          try {
  92              $number = Helpers::validateNumericNullBool($number);
  93              $significance = Helpers::validateNumericNullSubstitution($significance, null);
  94          } catch (Exception $e) {
  95              return $e->getMessage();
  96          }
  97  
  98          if (!$significance) {
  99              return 0.0;
 100          }
 101          $result = $number / abs($significance);
 102  
 103          return ceil($result) * $significance * (($significance < 0) ? -1 : 1);
 104      }
 105  
 106      /**
 107       * Let CEILINGMATH complexity pass Scrutinizer.
 108       */
 109      private static function ceilingMathTest(float $significance, float $number, int $mode): bool
 110      {
 111          return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode));
 112      }
 113  
 114      /**
 115       * Avoid Scrutinizer problems concerning complexity.
 116       *
 117       * @return float|string
 118       */
 119      private static function argumentsOk(float $number, float $significance)
 120      {
 121          if (empty($number * $significance)) {
 122              return 0.0;
 123          }
 124          if (Helpers::returnSign($number) == Helpers::returnSign($significance)) {
 125              return ceil($number / $significance) * $significance;
 126          }
 127  
 128          return Functions::NAN();
 129      }
 130  
 131      private static function floorCheck1Arg(): void
 132      {
 133          $compatibility = Functions::getCompatibilityMode();
 134          if ($compatibility === Functions::COMPATIBILITY_EXCEL) {
 135              throw new Exception('Excel requires 2 arguments for CEILING');
 136          }
 137      }
 138  }