How I will get ratio of 2 number in php

Discussion in 'PHP' started by pradipkeya, Jul 17, 2008.

  1. #1
    Hi I am learning php and getting some problem in calculation the ratio of 2 number.


    Suppose I have in $Var1 = 100
    and in $var2 = 40

    And I want to show the output 5:2



    Any help will be appreciated ,

    Thanks,
    Pradip
     
    pradipkeya, Jul 17, 2008 IP
  2. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #2
    $result1= $Var1 / $Var2;

    $result2= $Var1 % $Var2;

    This will produce:

    $result1 = 2.5
    $result2 = 20
     
    Kendothpro, Jul 17, 2008 IP
  3. pradipkeya

    pradipkeya Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Sir answer should not be 2.5 : 20. It should be 5 : 2 right??


    Thanks,
    Pradip
     
    pradipkeya, Jul 17, 2008 IP
  4. pradipkeya

    pradipkeya Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Knock Knock any one here to help me out to solve this problem. Please please please :)
     
    pradipkeya, Jul 17, 2008 IP
  5. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #5
    
    $var1 = 100;
    $var2 = 40; 
    
    $den=(int) ($var1 / $var2);  
    $ratio=$var2/$den; 
    $num=$var1/$ratio; 
    
    PHP:
    $num and $den are the two answers :p
     
    Kendothpro, Jul 17, 2008 IP
  6. pradipkeya

    pradipkeya Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    No the logic is not correct just use

    $var1 = 100;
    $var2 = 50;

    The ratio should be 2:1 But according to your logic it is giving 4:2 :(

    Thanks,
    Pradip
     
    pradipkeya, Jul 17, 2008 IP
  7. nihcer

    nihcer Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    try this:

    $var1=12;
    $var2=40;
    
    for($x=$var2;$x>1;$x--) {
      if(($var1%$x)==0 && ($var2 % $x)==0) {
        $var1 = $var1/$x;
        $var2 = $var2/$x;
      }
    }
    echo "$var1 : $var2";
    PHP:
     
    nihcer, Jul 17, 2008 IP
  8. pradipkeya

    pradipkeya Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks a lot I am really great full to you :)

    Pradip
     
    pradipkeya, Jul 17, 2008 IP
  9. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #9
    The thing your looking for would be to find the greatest common Denominator of both numbers then output the results of dividing those numbers by the GCD.

    
    
    //CREDITS
    //============================================================
    //Author: J. A. Greant ( zak@nucleus.com )
    //Version 1: June 9, 2000
    //http://www.weberdev.com/get_example-1808.html
    //============================================================ 
    function GCD ($a, $b)
    {
      while ( $b != 0)
      {
         $remainder = $a % $b;
         $a = $b;
         $b = $remainder;
      }
      return abs ($a);
    } 
    
    $a = 100;
    $b = 40;
    
    $var = GCD ($a,$b);
    
    print $a/$var .':'.$b/$var;
    
    
    PHP:

    Something of the above will work for you.
     
    exodus, Jul 17, 2008 IP