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 Base
   9  {
  10      /**
  11       * BASE.
  12       *
  13       * Converts a number into a text representation with the given radix (base).
  14       *
  15       * Excel Function:
  16       *        BASE(Number, Radix [Min_length])
  17       *
  18       * @param mixed $number expect float
  19       * @param mixed $radix expect float
  20       * @param mixed $minLength expect int or null
  21       *
  22       * @return string the text representation with the given radix (base)
  23       */
  24      public static function evaluate($number, $radix, $minLength = null)
  25      {
  26          try {
  27              $number = (float) floor(Helpers::validateNumericNullBool($number));
  28              $radix = (int) Helpers::validateNumericNullBool($radix);
  29          } catch (Exception $e) {
  30              return $e->getMessage();
  31          }
  32          $minLength = Functions::flattenSingleValue($minLength);
  33  
  34          if ($minLength === null || is_numeric($minLength)) {
  35              if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
  36                  return Functions::NAN(); // Numeric range constraints
  37              }
  38  
  39              $outcome = strtoupper((string) base_convert("$number", 10, $radix));
  40              if ($minLength !== null) {
  41                  $outcome = str_pad($outcome, (int) $minLength, '0', STR_PAD_LEFT); // String padding
  42              }
  43  
  44              return $outcome;
  45          }
  46  
  47          return Functions::VALUE();
  48      }
  49  }