SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: percentage

  1. #1
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    percentage

    Is there a function for PHP 3 / 4 that I can use that will get the percentage from two numbers? I have a quiz program, and say I got 4 ($yourscore) out of 5 ($board_config['questions_quiz']), what could I do to get a %? I have tried dividing then multiplying by 10, but it doesn't work at all like it should

    Thanks

  2. #2
    SitePoint Enthusiast Redprince's Avatar
    Join Date
    May 2004
    Location
    Salzgitter, Lower Saxony, Germany
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no function given, but this should work:
    Code:
    function percentage($a, $b)
    {
      return ($b / ($a / 100));
    }
    
    echo percentage(4, 5); // 80

  3. #3
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is $a and $b assigned to? And in the echo line do I replace 4, 5 with $a, $b

    Thanks for the code and help

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    To find the percentage score...
    PHP Code:
    $score $total_possible/100*$quiz_result
    where $total_possible is the total score possible and $quiz_result is the players result.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!! It worked!

  6. #6
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But that has nothing to do with PHP itself, it's just maths..

    60% is just the same as 0.6

    5.4% is 0.054

    To get 6.7% of a total of 775: 775 * 0.067

    Or to add 5% to a total of 120: 120 x 1.05

    .....

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    It may be basic maths, but it does what he needed. Just cos it ain't php don't mean it ain't right!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I knew the maths, but it turned out I was using the wrong symbol. I thought x was multiply when it was actually * in PHP

  9. #9
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by battye
    I knew the maths, but it turned out I was using the wrong symbol. I thought x was multiply when it was actually * in PHP
    Haha... Don't you hate those stupid errors? Glad you got it working.

  10. #10
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah haha, thanks

  11. #11
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since battye had his question answered, can I ask a question?

    What if I have a form where someone enters a percentage rate[tax] in the form of
    Code:
    Please enter tax: 5.9 [%]
    and I need to get the total of all items plus the tax. How do I tell php that the number 5.9 is actually a percent (0.059) so that the math is done correctly?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Use the form text entry as you would for posting variables....
    PHP Code:
    $percentage_entered $_POST['percentage_entered'];
    $tax $total_amount/100*$percentage_entered
    Make sense?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I feel very stupid. Didn't think of doing it that way. Sooo simple.

    Thanks!
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •