From 1fa6a97bba9a221b0652c6d8699e6529a971bb31 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 23 Mar 2020 09:09:11 -0400 Subject: [PATCH 1/2] Add a $separator parameter to the Fraction class's toDecimal() method. --- src/Fraction.php | 15 ++++++++++++--- src/FractionInterface.php | 5 ++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Fraction.php b/src/Fraction.php index 6048ac7..591fe7c 100644 --- a/src/Fraction.php +++ b/src/Fraction.php @@ -139,7 +139,7 @@ public function toString(string $separator = '/') { /** * {@inheritdoc} */ - public function toDecimal(int $precision = 0, bool $auto_precision = FALSE) { + public function toDecimal(int $precision = 0, bool $auto_precision = FALSE, string $separator = '.') { // Get the numerator and denominator. $numerator = $this->getNumerator(); @@ -175,13 +175,22 @@ public function toDecimal(int $precision = 0, bool $auto_precision = FALSE) { $value = bcdiv($numerator, $denominator, $precision + 1); // Return a decimal string rounded to the final precision. - return $this->bcRound($value, $precision); + $output = $this->bcRound($value, $precision); } // If BCMath is not available, use normal PHP float division and rounding. else { - return (string) round($numerator / $denominator, $precision); + $output = (string) round($numerator / $denominator, $precision); } + + // If the decimal separator character is not a period, perform a string + // replacement. + if ($separator !== '.') { + $output = str_replace('.', $separator, $output); + } + + // Return the decimal value. + return $output; } /** diff --git a/src/FractionInterface.php b/src/FractionInterface.php index 83b6036..39b4ab5 100644 --- a/src/FractionInterface.php +++ b/src/FractionInterface.php @@ -69,11 +69,14 @@ public function toString(string $separator = '/'); * don't. If set to TRUE, it will try to determine the maximum precision * (this only works if the denominator is base 10). If the resulting * precision is greater than $precision, it will be used instead. + * @param string $separator + * The character that should be used as a decimal separator. Defaults to a + * period. * * @return string * Returns the decimal equivalent of the fraction as a PHP string. */ - public function toDecimal(int $precision, bool $auto_precision = FALSE); + public function toDecimal(int $precision, bool $auto_precision = FALSE, string $separator = '.'); /** * Calculates the numerator and denominator from a decimal value. From 6e27a66ba83d855e52c8172f506009964da80121 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 23 Mar 2020 09:59:18 -0400 Subject: [PATCH 2/2] Add a $separator parameter to the Fraction class's createFromDecimal() method. --- src/Fraction.php | 4 ++-- src/FractionInterface.php | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Fraction.php b/src/Fraction.php index 591fe7c..c7beaef 100644 --- a/src/Fraction.php +++ b/src/Fraction.php @@ -45,10 +45,10 @@ public function fromDecimal($value) { /** * {@inheritdoc} */ - public static function createFromDecimal($value) { + public static function createFromDecimal($value, string $separator = '.') { // Calculate the precision by counting the number of decimal places. - $precision = strlen(substr(strrchr($value, '.'), 1)); + $precision = strlen(substr(strrchr($value, $separator), 1)); // Create the denominator by raising 10 to the power of the precision. if (function_exists('bcpow')) { diff --git a/src/FractionInterface.php b/src/FractionInterface.php index 39b4ab5..02c5519 100644 --- a/src/FractionInterface.php +++ b/src/FractionInterface.php @@ -97,11 +97,14 @@ public function fromDecimal($value); * * @param string|int $value * The decimal value to start with. + * @param string $separator + * The character that is used as a decimal separator. If this is not set, + * then the default separator is assumed to be a period. * * @return Fraction * Returns this object. */ - public static function createFromDecimal($value); + public static function createFromDecimal($value, string $separator = '.'); /** * Calculate the fraction's greatest common divisor using Euclid's algorithm.