wc_decimal_to_fraction()
Convert a decimal (e.g. 3.5) to a fraction (e.g. 7/2). From: https://www.designedbyaturtle.co.uk/2015/converting-a-decimal-to-a-fraction-in-php/
No Hooks.
Returns
Array|true|false. a 1/2 would be [1, 2] array (this can be imploded with '/' to form a string).
Usage
wc_decimal_to_fraction( $decimal );
- $decimal(float) (required)
- the decimal number.
wc_decimal_to_fraction() wc decimal to fraction code WC 10.4.3
function wc_decimal_to_fraction( $decimal ) {
if ( 0 > $decimal || ! is_numeric( $decimal ) ) {
// Negative digits need to be passed in as positive numbers and prefixed as negative once the response is imploded.
return false;
}
if ( 0 === $decimal ) {
return array( 0, 1 );
}
$tolerance = 1.e-4;
$numerator = 1;
$h2 = 0;
$denominator = 0;
$k2 = 1;
$b = 1 / $decimal;
do {
$b = 1 / $b;
$a = floor( $b );
$aux = $numerator;
$numerator = $a * $numerator + $h2;
$h2 = $aux;
$aux = $denominator;
$denominator = $a * $denominator + $k2;
$k2 = $aux;
$b = $b - $a;
} while ( abs( $decimal - $numerator / $denominator ) > $decimal * $tolerance );
return array( $numerator, $denominator );
}