| 1441 |
ariadna |
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 If an array of values is passed for either of the arguments, then the returned result
|
|
|
29 |
* will also be an array with matching dimensions
|
|
|
30 |
*/
|
|
|
31 |
public static function format(mixed $number, mixed $precision = 2)
|
|
|
32 |
{
|
|
|
33 |
return Format::DOLLAR($number, $precision);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* DOLLARDE.
|
|
|
38 |
*
|
|
|
39 |
* Converts a dollar price expressed as an integer part and a fraction
|
|
|
40 |
* part into a dollar price expressed as a decimal number.
|
|
|
41 |
* Fractional dollar numbers are sometimes used for security prices.
|
|
|
42 |
*
|
|
|
43 |
* Excel Function:
|
|
|
44 |
* DOLLARDE(fractional_dollar,fraction)
|
|
|
45 |
*
|
|
|
46 |
* @param mixed $fractionalDollar Fractional Dollar
|
|
|
47 |
* Or can be an array of values
|
|
|
48 |
* @param mixed $fraction Fraction
|
|
|
49 |
* Or can be an array of values
|
|
|
50 |
*/
|
|
|
51 |
public static function decimal(mixed $fractionalDollar = null, mixed $fraction = 0): array|string|float
|
|
|
52 |
{
|
|
|
53 |
if (is_array($fractionalDollar) || is_array($fraction)) {
|
|
|
54 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $fractionalDollar, $fraction);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
try {
|
|
|
58 |
$fractionalDollar = FinancialValidations::validateFloat(
|
|
|
59 |
Functions::flattenSingleValue($fractionalDollar) ?? 0.0
|
|
|
60 |
);
|
|
|
61 |
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
|
|
|
62 |
} catch (Exception $e) {
|
|
|
63 |
return $e->getMessage();
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// Additional parameter validations
|
|
|
67 |
if ($fraction < 0) {
|
|
|
68 |
return ExcelError::NAN();
|
|
|
69 |
}
|
|
|
70 |
if ($fraction == 0) {
|
|
|
71 |
return ExcelError::DIV0();
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$dollars = ($fractionalDollar < 0) ? ceil($fractionalDollar) : floor($fractionalDollar);
|
|
|
75 |
$cents = fmod($fractionalDollar, 1.0);
|
|
|
76 |
$cents /= $fraction;
|
|
|
77 |
$cents *= 10 ** ceil(log10($fraction));
|
|
|
78 |
|
|
|
79 |
return $dollars + $cents;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* DOLLARFR.
|
|
|
84 |
*
|
|
|
85 |
* Converts a dollar price expressed as a decimal number into a dollar price
|
|
|
86 |
* expressed as a fraction.
|
|
|
87 |
* Fractional dollar numbers are sometimes used for security prices.
|
|
|
88 |
*
|
|
|
89 |
* Excel Function:
|
|
|
90 |
* DOLLARFR(decimal_dollar,fraction)
|
|
|
91 |
*
|
|
|
92 |
* @param mixed $decimalDollar Decimal Dollar
|
|
|
93 |
* Or can be an array of values
|
|
|
94 |
* @param mixed $fraction Fraction
|
|
|
95 |
* Or can be an array of values
|
|
|
96 |
*/
|
|
|
97 |
public static function fractional(mixed $decimalDollar = null, mixed $fraction = 0): array|string|float
|
|
|
98 |
{
|
|
|
99 |
if (is_array($decimalDollar) || is_array($fraction)) {
|
|
|
100 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $decimalDollar, $fraction);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
try {
|
|
|
104 |
$decimalDollar = FinancialValidations::validateFloat(
|
|
|
105 |
Functions::flattenSingleValue($decimalDollar) ?? 0.0
|
|
|
106 |
);
|
|
|
107 |
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction));
|
|
|
108 |
} catch (Exception $e) {
|
|
|
109 |
return $e->getMessage();
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Additional parameter validations
|
|
|
113 |
if ($fraction < 0) {
|
|
|
114 |
return ExcelError::NAN();
|
|
|
115 |
}
|
|
|
116 |
if ($fraction == 0) {
|
|
|
117 |
return ExcelError::DIV0();
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$dollars = ($decimalDollar < 0.0) ? ceil($decimalDollar) : floor($decimalDollar);
|
|
|
121 |
$cents = fmod($decimalDollar, 1);
|
|
|
122 |
$cents *= $fraction;
|
|
|
123 |
$cents *= 10 ** (-ceil(log10($fraction)));
|
|
|
124 |
|
|
|
125 |
return $dollars + $cents;
|
|
|
126 |
}
|
|
|
127 |
}
|