3v4l.org

run code in 300+ PHP versions simultaneously
<?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 git.master, git.master_jit, rfc.property-hooks
1 1/2

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
40.42 ms | 401 KiB | 8 Q