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\Financial;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  use PhpOffice\PhpSpreadsheet\Calculation\TextData\Format;
   7  
   8  class Dollar
   9  {
  10      /**
  11       * DOLLAR.
  12       *
  13       * This function converts a number to text using currency format, with the decimals rounded to the specified place.
  14       * The format used is $#,##0.00_);($#,##0.00)..
  15       *
  16       * @param mixed $number The value to format
  17       * @param mixed $precision The number of digits to display to the right of the decimal point (as an integer).
  18       *                            If precision is negative, number is rounded to the left of the decimal point.
  19       *                            If you omit precision, it is assumed to be 2
  20       */
  21      public static function format($number, $precision = 2): string
  22      {
  23          return Format::DOLLAR($number, $precision);
  24      }
  25  
  26      /**
  27       * DOLLARDE.
  28       *
  29       * Converts a dollar price expressed as an integer part and a fraction
  30       *        part into a dollar price expressed as a decimal number.
  31       * Fractional dollar numbers are sometimes used for security prices.
  32       *
  33       * Excel Function:
  34       *        DOLLARDE(fractional_dollar,fraction)
  35       *
  36       * @param mixed $fractionalDollar Fractional Dollar
  37       * @param mixed $fraction Fraction
  38       *
  39       * @return float|string
  40       */
  41      public static function decimal($fractionalDollar = null, $fraction = 0)
  42      {
  43          $fractionalDollar = Functions::flattenSingleValue($fractionalDollar);
  44          $fraction = (int) Functions::flattenSingleValue($fraction);
  45  
  46          // Validate parameters
  47          if ($fractionalDollar === null || $fraction < 0) {
  48              return Functions::NAN();
  49          }
  50          if ($fraction == 0) {
  51              return Functions::DIV0();
  52          }
  53  
  54          $dollars = floor($fractionalDollar);
  55          $cents = fmod($fractionalDollar, 1);
  56          $cents /= $fraction;
  57          $cents *= 10 ** ceil(log10($fraction));
  58  
  59          return $dollars + $cents;
  60      }
  61  
  62      /**
  63       * DOLLARFR.
  64       *
  65       * Converts a dollar price expressed as a decimal number into a dollar price
  66       *        expressed as a fraction.
  67       * Fractional dollar numbers are sometimes used for security prices.
  68       *
  69       * Excel Function:
  70       *        DOLLARFR(decimal_dollar,fraction)
  71       *
  72       * @param mixed $decimalDollar Decimal Dollar
  73       * @param mixed $fraction Fraction
  74       *
  75       * @return float|string
  76       */
  77      public static function fractional($decimalDollar = null, $fraction = 0)
  78      {
  79          $decimalDollar = Functions::flattenSingleValue($decimalDollar);
  80          $fraction = (int) Functions::flattenSingleValue($fraction);
  81  
  82          // Validate parameters
  83          if ($decimalDollar === null || $fraction < 0) {
  84              return Functions::NAN();
  85          }
  86          if ($fraction == 0) {
  87              return Functions::DIV0();
  88          }
  89  
  90          $dollars = floor($decimalDollar);
  91          $cents = fmod($decimalDollar, 1);
  92          $cents *= $fraction;
  93          $cents *= 10 ** (-ceil(log10($fraction)));
  94  
  95          return $dollars + $cents;
  96      }
  97  }