3v4l.org

run code in 300+ PHP versions simultaneously
<?php $whole = floor ( $float ); $decimal = $float - $whole; $leastCommonDenom = 48; // 16 * 3; $denominators = array (2, 3, 4, 8, 16, 24, 48 ); $roundedDecimal = round ( $decimal * $leastCommonDenom ) / $leastCommonDenom; if ($roundedDecimal == 0) return $whole; if ($roundedDecimal == 1) return $whole + 1; foreach ( $denominators as $d ) { if ($roundedDecimal * $d == floor ( $roundedDecimal * $d )) { $denom = $d; break; } } return ($whole == 0 ? '' : $whole) . " " . ($roundedDecimal * $denom) . "/" . $denom; //echo toFraction( -0.5) . "\n" . toFraction( 1.5);

preferences:
54.88 ms | 402 KiB | 5 Q