Re: Fractions

From: Date: Tue, 10 Dec 2002 20:36:33 +0000
Subject: Re: Fractions
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-127717@lists.php.net to get a copy of this message
Stephen wrote:
I found how it works, and it doesn't put it into a mixed number. I work on it this week and see if I can't fix it. Thanks for all the help people! ----- Original Message ----- From: "Andrew Brampton" <andrew@bramp.freeserve.co.uk> To: "Stephen" <webmaster@melchior.us> Sent: Monday, December 09, 2002 7:17 PM Subject: Re: [PHP] Fractions
how about you try it to see what happens, and maybe learn how it works Andrew ----- Original Message ----- From: "Stephen" <webmaster@melchior.us> To: "Andrew Brampton" <andrew@bramp.freeserve.co.uk> Cc: "PHP List" <php-general@lists.php.net> Sent: Tuesday, December 10, 2002 12:11 AM Subject: Re: [PHP] Fractions
Would this also print out as a mixed number if it should? ----- Original Message ----- From: "Andrew Brampton" <andrew@bramp.freeserve.co.uk> To: <php-general@lists.php.net> Sent: Monday, December 09, 2002 6:44 PM Subject: Re: [PHP] Fractions
Well how would you do it on paper? $numerator = 8; $denominator = 12; $factor = 1; //Start at the greater of the 2, and loop down til you find a common
factor
for ($i=max($numerator,$denominator);$i>1;$i--) { //Check if each number divided by $i has no remainder if (($numerator % $i) == 0 && ($denominator % $i) == 0) {
       //Factor Found
       $factor = $i;
       break;
       }
} //Now a factor is found, divide by it $numerator = $numerator / $factor; $denominator = $denominator / $factor; echo $numerator . '/' . $denominator; The method might look like the above Andrew ----- Original Message ----- From: "Stephen" <webmaster@melchior.us> To: "Ray Hunter" <bigdog@venticon.com> Cc: "PHP List" <php-general@lists.php.net> Sent: Monday, December 09, 2002 11:20 PM Subject: Re: [PHP] Fractions
But how do you find it in PHP? ----- Original Message ----- From: "Ray Hunter" <bigdog@venticon.com> To: "Stephen" <webmaster@melchior.us> Cc: <php-general@lists.php.net> Sent: Monday, December 09, 2002 6:12 PM Subject: Re: [PHP] Fractions
Just like math...find the greatest common denominator of the
numerator
and denominator and then divide each (numerator and denominator)
by
that
number... ie: numerator = 8 and denominator = 12 so we have 8/12 then greatest common denominator is 4... so 8/4 = 2 and 12/4 = 3 thus, 8/12 => 2/3 On Mon, 2002-12-09 at 15:55, Stephen wrote:
I know for a fact that you're all going to think, "What the heck
does
he want fractions for!?" I have a reason...I just won't tell
you.
:-P
My problem is this. I want to simplify a fraction to simplest
form
but
if I divide, I'll get a decimal which I can't use. How could I
put
it
in simplest form without displaying a decimal to the user and if
one
does come up, a mixed number? Thanks, Stephen Craton http://www.melchior.us "What is a dreamer that cannot persevere?" --
http://www.melchior.us
______________________________________________________________________
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- Ray Hunter email: bigdog@venticon.com www: http://venticon.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
I use this to convert a decimal stored in a table.
function Fraction ($width) {
    list ($whole, $numerator) = explode ('.', $width);
    $denominator = 1 . str_repeat (0, strlen ($numerator));
    $GCD = GCD ($numerator, $denominator);
    $numerator /= $GCD;
    $denominator /= $GCD;
    If ($numerator==0) {
    return sprintf ('%d',$whole);
} else {
    return sprintf ('%d %d/%d',
        $whole, $numerator, $denominator);
} } function GCD ($a, $b) {
    while ( $b != 0)
      {
        $remainder = $a % $b;
        $a = $b;
        $b = $remainder;
      }
    return abs ($a);
} So... $width = 64.75; Fraction($width); Results: 64 3/4 You can add <sup></sup> and <sub></sub> for added effect.

« previous php.general (#127717) next »