I'm writing a script to scale recipes for my wife. In the process, I'm trying to teach this @#$ZGT123 computer how to do 3rd grade math.
Here's the brute force way of converting decimals to fractions. Fortunately, I only need things rounded to the nearest 1/8th. Figuring out how to convert Tbsp's into tsp's might be a little challenging
$Scale = $CalcNum * $Factor;
$Deno = (int)($Scale);
$Rounded = round($Scale, 3);
$Frac = $Rounded - $Deno;
echo " Scale: " . $Scale . " Frac: " . $Frac;
if ($Frac > 0 && $Frac<=.125) $Fraction = "1/8";
if ($Frac > .125 && $Frac<= 0.25) $Fraction = "1/4";
if ($Frac > .25 && $Frac <= 0.333) $Fraction = "1/3";
if ($Frac > .333 && $Frac <= .375) $Fraction = "3/8";
if ($Frac > .375 && $Frac <= 0.5) $Fraction = "1/2";
if ($Frac > .5 && Frac <= 0.625) $Fraction = "5/8";
if ($Frac > .625 && $Frac <= 0.667) $Fraction = "2/3";
if ($Frac > .667 && $Frac <= 0.75) $Fraction = "3/4";
if ($Frac > .75 && $Frac <= 0.875) $Fraction = "7/8";
if ($Frac > .875 && $Frac <= 1)
{
$Deno++;
$Frac=0;
}
if ($Deno > 0) $NewQty = $Deno . " " . $Fraction;
if ($Deno==0) $NewQty = $Fraction;
if($Frac == 0) $NewQty = $Deno;
echo " New: " . $NewQty;
echo "<br /><br />";
I'm getting one result that don't make sense. When
$CalcNum = 3.5 & $Factor = .667, $Scale = 2.333345
$Rounded = .333
$Frac = .333
$Fraction = "3/8" ??? Should be "1/3";
However, on my calculator,
$Scale = 2.3345 then $Frac = .335, yielding $Fraction as "3/8" instead of "1/3".
Why the discrepancy between the big box and the little one?
tim
"If builders built buildings the way that programmers write programs, the first woodpecker to come along would wreck civilization."