Improve repeating fractionals workaround

This commit is contained in:
Christopher C. Wells 2021-01-15 06:13:32 -08:00
parent 81f590515d
commit 63f22274a5
1 changed files with 3 additions and 7 deletions

View File

@ -29,7 +29,7 @@ class Number
/** /**
* Get a string faction representation of a float. * Get a string faction representation of a float.
* *
* @todo Handle repeating values like 1/3, 2/3, etc. * @todo Handle repeating values like 1/3, 2/3, etc. better.
* *
* @see https://rosettacode.org/wiki/Convert_decimal_number_to_rational#PHP * @see https://rosettacode.org/wiki/Convert_decimal_number_to_rational#PHP
* *
@ -40,12 +40,8 @@ class Number
*/ */
public static function fractionStringFromFloat(float $value): string { public static function fractionStringFromFloat(float $value): string {
$fraction = (string) Fraction::fromFloat($value); $fraction = (string) Fraction::fromFloat($value);
if ($fraction === '33333333/100000000') { $fraction = str_replace('33333333/100000000', '1/3', $fraction);
$fraction = '1/3'; $fraction = str_replace('66666667/100000000', '2/3', $fraction);
}
elseif ($fraction === '66666667/100000000') {
$fraction = '2/3';
}
return $fraction; return $fraction;
} }
} }