3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function decToFraction($float) { // 1/2, 1/4, 1/8, 1/16, 1/3 ,2/3, 3/4, 3/8, 5/8, 7/8, 3/16, 5/16, 7/16, // 9/16, 11/16, 13/16, 15/16 $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 decToFraction(1.5); //echo toFraction( -0.5) . "\n" . toFraction( 1.5);
Output for 4.3.0 - 5.0.5, 5.1.1 - 7.1.0
1 1/2
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/tsJR5 on line 24
Process exited with code 255.