Decimal to Fraction
Results 1 to 4 of 4

Thread: Decimal to Fraction

  1. #1
    Senior Member
    Join Date
    Sep 2012
    Posts
    269

    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($Scale3);
                    
    $Frac $Rounded $Deno;
                    
                    
                    echo 
    " Scale: " $Scale " Frac: " $Frac;
                    
                    
                    if (
    $Frac && $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 "&nbsp" $Fraction;
                    if (
    $Deno==0$NewQty $Fraction;
                    if(
    $Frac == 0$NewQty $Deno;
                    
                    echo 
    "&nbsp;New:  &nbsp;" $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.

  2. #2
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by timstring View Post
    ...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.

  3. #3
    Senior Member
    Join Date
    Mar 2009
    Location
    Canada
    Posts
    794
    This is why I wish everything was in metric.

    Quote Originally Posted by timstring View Post
    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.
    Prison of Mirrors
    Declare variables, not war.

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    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 && $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 && $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
  •