Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Created February 24, 2014 08:57
Show Gist options
  • Save doublejosh/9184115 to your computer and use it in GitHub Desktop.
Save doublejosh/9184115 to your computer and use it in GitHub Desktop.
/**
* 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