This code will convert a decimal to it's fraction equivalent. The precision can be set by changing PRECISION.
<?php
define(PRECISION, .01);
$count=0;
$result=array();
decimalToFraction($_REQUEST['dec'],$count,&$result);
$count = count($result);
$simp_fract = simplifyFraction($result,$count,1,$result[$count]);
echo $simpl_fract;
function decimalToFraction($decimal,$count,$result) {
$a = (1/$decimal);
$b = ( $a - floor($a) );
$count++;
if ($b > .01 && $count <= 5) decimalToFraction($b,$count,&$result);
$result[$count] = floor($a);
}
function simplifyFraction($fraction,$count,$top,$bottom) {
$next = $fraction[$count-1];
$a = ($bottom * $next) + $top;
$top = $bottom;
$bottom = $a;
$count--;
if ($count > 0) simplifyFraction($fraction,$count,$top,$bottom);
else {
return "<font size=1>$bottom/$top</font>";
}
}
?>