Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception;
   6  
   7  class Number extends NumberBase implements Wizard
   8  {
   9      public const WITH_THOUSANDS_SEPARATOR = true;
  10  
  11      public const WITHOUT_THOUSANDS_SEPARATOR = false;
  12  
  13      protected bool $thousandsSeparator = true;
  14  
  15      /**
  16       * @param int $decimals number of decimal places to display, in the range 0-30
  17       * @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
  18       * @param ?string $locale Set the locale for the number format; or leave as the default null.
  19       *          Locale has no effect for Number Format values, and is retained here only for compatibility
  20       *              with the other Wizards.
  21       *          If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
  22       *
  23       * @throws Exception If a provided locale code is not a valid format
  24       */
  25      public function __construct(
  26          int $decimals = 2,
  27          bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR,
  28          ?string $locale = null
  29      ) {
  30          $this->setDecimals($decimals);
  31          $this->setThousandsSeparator($thousandsSeparator);
  32          $this->setLocale($locale);
  33      }
  34  
  35      public function setThousandsSeparator(bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR): void
  36      {
  37          $this->thousandsSeparator = $thousandsSeparator;
  38      }
  39  
  40      /**
  41       * As MS Excel cannot easily handle Lakh, which is the only locale-specific Number format variant,
  42       *       we don't use locale with Numbers.
  43       */
  44      protected function getLocaleFormat(): string
  45      {
  46          return $this->format();
  47      }
  48  
  49      public function format(): string
  50      {
  51          return sprintf(
  52              '%s0%s',
  53              $this->thousandsSeparator ? '#,##' : null,
  54              $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null
  55          );
  56      }
  57  }