-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start work on breaking down some of the Financial Excel functions
- Loading branch information
MarkBaker
committed
Mar 19, 2021
1 parent
4e8a926
commit 5ad1675
Showing
3 changed files
with
92 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; | ||
|
||
class Dollar | ||
{ | ||
/** | ||
* 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 float $fractionalDollar Fractional Dollar | ||
* @param int $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 float $decimalDollar Decimal Dollar | ||
* @param int $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; | ||
} | ||
} |