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\Financial;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   9  use PhpOffice\PhpSpreadsheet\Calculation\TextData\Format;
  10  
  11  class Dollar
  12  {
  13      use ArrayEnabled;
  14  
  15      /**
  16       * DOLLAR.
  17       *
  18       * This function converts a number to text using currency format, with the decimals rounded to the specified place.
  19       * The format used is $#,##0.00_);($#,##0.00)..
  20       *
  21       * @param mixed $number The value to format, or can be an array of numbers
  22       *                         Or can be an array of values
  23       * @param mixed $precision The number of digits to display to the right of the decimal point (as an integer).
  24       *                            If precision is negative, number is rounded to the left of the decimal point.
  25       *                            If you omit precision, it is assumed to be 2
  26       *              Or can be an array of precision values
  27       *
  28       * @return array|string
  29       *         If an array of values is passed for either of the arguments, then the returned result
  30       *            will also be an array with matching dimensions
  31       */
  32      public static function format($number, $precision = 2)
  33      {
  34          return Format::DOLLAR($number, $precision);
  35      }
  36  
  37      /**
  38       * DOLLARDE.
  39       *
  40       * Converts a dollar price expressed as an integer part and a fraction
  41       *        part into a dollar price expressed as a decimal number.
  42       * Fractional dollar numbers are sometimes used for security prices.
  43       *
  44       * Excel Function:
  45       *        DOLLARDE(fractional_dollar,fraction)
  46       *
  47       * @param mixed $fractionalDollar Fractional Dollar
  48       *              Or can be an array of values
  49       * @param mixed $fraction Fraction
  50       *              Or can be an array of values
  51       *
  52       * @return array|float|string
  53       */
  54      public static function decimal($fractionalDollar = null, $fraction = 0)
  55      {
  56          if (is_array($fractionalDollar) || is_array($fraction)) {
  57              return self::evaluateArrayArguments([self::class, __FUNCTION__], $fractionalDollar, $fraction);
  58          }
  59  
  60          try {
  61              $fractionalDollar = FinancialValidations::validateFloat(
  62                  Functions::flattenSingleValue($fractionalDollar) ?? 0.0
  63              );
  64              $fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
  65          } catch (Exception $e) {
  66              return $e->getMessage();
  67          }
  68  
  69          // Additional parameter validations
  70          if ($fraction < 0) {
  71              return ExcelError::NAN();
  72          }
  73          if ($fraction == 0) {
  74              return ExcelError::DIV0();
  75          }
  76  
  77          $dollars = ($fractionalDollar < 0) ? ceil($fractionalDollar) : floor($fractionalDollar);
  78          $cents = fmod($fractionalDollar, 1.0);
  79          $cents /= $fraction;
  80          $cents *= 10 ** ceil(log10($fraction));
  81  
  82          return $dollars + $cents;
  83      }
  84  
  85      /**
  86       * DOLLARFR.
  87       *
  88       * Converts a dollar price expressed as a decimal number into a dollar price
  89       *        expressed as a fraction.
  90       * Fractional dollar numbers are sometimes used for security prices.
  91       *
  92       * Excel Function:
  93       *        DOLLARFR(decimal_dollar,fraction)
  94       *
  95       * @param mixed $decimalDollar Decimal Dollar
  96       *              Or can be an array of values
  97       * @param mixed $fraction Fraction
  98       *              Or can be an array of values
  99       *
 100       * @return array|float|string
 101       */
 102      public static function fractional($decimalDollar = null, $fraction = 0)
 103      {
 104          if (is_array($decimalDollar) || is_array($fraction)) {
 105              return self::evaluateArrayArguments([self::class, __FUNCTION__], $decimalDollar, $fraction);
 106          }
 107  
 108          try {
 109              $decimalDollar = FinancialValidations::validateFloat(
 110                  Functions::flattenSingleValue($decimalDollar) ?? 0.0
 111              );
 112              $fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
 113          } catch (Exception $e) {
 114              return $e->getMessage();
 115          }
 116  
 117          // Additional parameter validations
 118          if ($fraction < 0) {
 119              return ExcelError::NAN();
 120          }
 121          if ($fraction == 0) {
 122              return ExcelError::DIV0();
 123          }
 124  
 125          $dollars = ($decimalDollar < 0.0) ? ceil($decimalDollar) : floor($decimalDollar);
 126          $cents = fmod($decimalDollar, 1);
 127          $cents *= $fraction;
 128          $cents *= 10 ** (-ceil(log10($fraction)));
 129  
 130          return $dollars + $cents;
 131      }
 132  }