Skip to content

Decimal separator #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Fraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/FractionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -94,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.
Expand Down