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\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  
   8  class Trunc
   9  {
  10      use ArrayEnabled;
  11  
  12      /**
  13       * TRUNC.
  14       *
  15       * Truncates value to the number of fractional digits by number_digits.
  16       *
  17       * @param array|float $value
  18       *                      Or can be an array of values
  19       * @param array|int $digits
  20       *                      Or can be an array of values
  21       *
  22       * @return array|float|string Truncated value, or a string containing an error
  23       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  24       *            with the same dimensions
  25       */
  26      public static function evaluate($value = 0, $digits = 0)
  27      {
  28          if (is_array($value) || is_array($digits)) {
  29              return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $digits);
  30          }
  31  
  32          try {
  33              $value = Helpers::validateNumericNullBool($value);
  34              $digits = Helpers::validateNumericNullSubstitution($digits, null);
  35          } catch (Exception $e) {
  36              return $e->getMessage();
  37          }
  38  
  39          $digits = floor($digits);
  40  
  41          // Truncate
  42          $adjust = 10 ** $digits;
  43  
  44          if (($digits > 0) && (rtrim((string) (int) ((abs($value) - abs((int) $value)) * $adjust), '0') < $adjust / 10)) {
  45              return $value;
  46          }
  47  
  48          return ((int) ($value * $adjust)) / $adjust;
  49      }
  50  }