Created
February 24, 2014 08:57
-
-
Save doublejosh/9184115 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Turn decimals into fractions. | |
*/ | |
function dec2frac($f) { | |
$base = floor($f); | |
if ($base) { | |
$out = $base . ' '; | |
$f = $f - $base; | |
} | |
if ($f != 0) { | |
$d = 1; | |
while (fmod($f, 1) != 0.0) { | |
$f *= 2; | |
$d *= 2; | |
} | |
$n = sprintf('%.0f', $f); | |
$d = sprintf('%.0f', $d); | |
$out .= $n . '/' . $d; | |
} | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment