Decimal to Fraction
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
PHP Code:
$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."
Last edited by timstring; 12-04-2012 at 01:25 AM .
Senior Member
Originally Posted by
timstring
...When $CalcNum = 3.5 & $Factor = .667, $Scale = 2.333345
...you sure about that ?
protip: computers don't have 3rd-grade brains.
try it for yourself:
PHP Code:
<?php
$Scale = 3.5 * .667 ;
print $Scale ;
Last edited by traq; 12-04-2012 at 01:47 AM .
This is why I wish everything was in metric.
Originally Posted by
timstring
Figuring out how to convert Tbsp's into tsp's might be a little challenging
Actually that's the easy part. A tablespoon is three teaspoons.
Senior Member
Your current code will work "well enough" for your purposes if you just adjust your scale a bit - round to the nearest desired fraction, instead of always rounding up; i.e., instead of this:
PHP Code:
if ( $Frac > 0 && $Frac <= .1875 ) $Fraction = "1/8" ;
if ( $Frac > .125 && $Frac <= 0.25 ) $Fraction = "1/4" ;
if ( $Frac > .25 && $Frac <= 0.333 ) $Fraction = "1/3" ;
// . . .etc.
use something like this:
PHP Code:
if ( $Frac > 0 && $Frac <= .125 ) $Fraction = "1/8" ;
if ( $Frac > .1875 && $Frac <= 0.2915 ) $Fraction = "1/4" ;
if ( $Frac > .2915 && $Frac <= 0.354 ) $Fraction = "1/3" ;
// . . .etc.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks