| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 9 |
use TablePress\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<mixed>|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($number, $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 |
* @return array<mixed>|float|string |
| 52 |
*/ |
| 53 |
public static function decimal($fractionalDollar = null, $fraction = 0) |
| 54 |
{ |
| 55 |
if (is_array($fractionalDollar) || is_array($fraction)) { |
| 56 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $fractionalDollar, $fraction); |
| 57 |
} |
| 58 |
|
| 59 |
try { |
| 60 |
$fractionalDollar = FinancialValidations::validateFloat( |
| 61 |
Functions::flattenSingleValue($fractionalDollar) ?? 0.0 |
| 62 |
); |
| 63 |
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction)); |
| 64 |
} catch (Exception $e) { |
| 65 |
return $e->getMessage(); |
| 66 |
} |
| 67 |
|
| 68 |
// Additional parameter validations |
| 69 |
if ($fraction < 0) { |
| 70 |
return ExcelError::NAN(); |
| 71 |
} |
| 72 |
if ($fraction == 0) { |
| 73 |
return ExcelError::DIV0(); |
| 74 |
} |
| 75 |
|
| 76 |
$dollars = ($fractionalDollar < 0) ? ceil($fractionalDollar) : floor($fractionalDollar); |
| 77 |
$cents = fmod($fractionalDollar, 1.0); |
| 78 |
$cents /= $fraction; |
| 79 |
$cents *= 10 ** ceil(log10($fraction)); |
| 80 |
|
| 81 |
return $dollars + $cents; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* DOLLARFR. |
| 86 |
* |
| 87 |
* Converts a dollar price expressed as a decimal number into a dollar price |
| 88 |
* expressed as a fraction. |
| 89 |
* Fractional dollar numbers are sometimes used for security prices. |
| 90 |
* |
| 91 |
* Excel Function: |
| 92 |
* DOLLARFR(decimal_dollar,fraction) |
| 93 |
* |
| 94 |
* @param mixed $decimalDollar Decimal Dollar |
| 95 |
* Or can be an array of values |
| 96 |
* @param mixed $fraction Fraction |
| 97 |
* Or can be an array of values |
| 98 |
* |
| 99 |
* @return array<mixed>|float|string |
| 100 |
*/ |
| 101 |
public static function fractional($decimalDollar = null, $fraction = 0) |
| 102 |
{ |
| 103 |
if (is_array($decimalDollar) || is_array($fraction)) { |
| 104 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $decimalDollar, $fraction); |
| 105 |
} |
| 106 |
|
| 107 |
try { |
| 108 |
$decimalDollar = FinancialValidations::validateFloat( |
| 109 |
Functions::flattenSingleValue($decimalDollar) ?? 0.0 |
| 110 |
); |
| 111 |
$fraction = FinancialValidations::validateInt(Functions::flattenSingleValue($fraction)); |
| 112 |
} catch (Exception $e) { |
| 113 |
return $e->getMessage(); |
| 114 |
} |
| 115 |
|
| 116 |
// Additional parameter validations |
| 117 |
if ($fraction < 0) { |
| 118 |
return ExcelError::NAN(); |
| 119 |
} |
| 120 |
if ($fraction == 0) { |
| 121 |
return ExcelError::DIV0(); |
| 122 |
} |
| 123 |
|
| 124 |
$dollars = ($decimalDollar < 0.0) ? ceil($decimalDollar) : floor($decimalDollar); |
| 125 |
$cents = fmod($decimalDollar, 1); |
| 126 |
$cents *= $fraction; |
| 127 |
$cents *= 10 ** (-ceil(log10($fraction))); |
| 128 |
|
| 129 |
return $dollars + $cents; |
| 130 |
} |
| 131 |
} |
| 132 |
|