Blame view

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