SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Calculations

Hybrid View

  1. #1
    SitePoint Evangelist hantaah's Avatar
    Join Date
    Jul 2011
    Location
    Birmingham, Uk
    Posts
    562
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Calculations

    I have a donation Centre set up with a goal of 100.000. So far 5 has been donated and the centre is showing it as 5% Done.

    I have this in my config.php
    Code:
    // Your goal in USD:
    $goal = 100.000;
    and I have this in my donate.php

    Code:
    // Calculating how many percent of the goal were met:
    $percent = round(min(100.000*($sum/$goal),100.000));
    What is missing or wrong?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There's nothing wrong - 5 is 5% of 100 so the answer is correct. The number of zeros after the decimal point doesn't change anything so I don't know why you are including three zeros there particularly since the third zero represents tenths of a penny.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,873
    Mentioned
    208 Post(s)
    Tagged
    12 Thread(s)
    Maybe the op means 100k?
    (In which case 100.000 should be written without the decimal point).

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Maybe the op means 100k?
    (In which case 100.000 should be written without the decimal point).
    Yeah, some places in europe use a decimal instead of a comma... either way it shouldn't be in there

  5. #5
    SitePoint Evangelist hantaah's Avatar
    Join Date
    Jul 2011
    Location
    Birmingham, Uk
    Posts
    562
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    yes that's right I mean 100k so I got rid of the decimal point and that changes it from 5% done to 0% done and changing the goal to 50000 makes it 5% done.
    Could someone explain to me what this code is for? What is the difference for the min100000 and the other 100000 as there are two 100000?

    Code:
    // Calculating how many percent of the goal were met:
    $percent = round(min(100000*($sum/$goal),100000));

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,873
    Mentioned
    208 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Have a look at this: http://www.phpbook.net/how-to-calcul...-with-php.html
    That should helpwith what you want to do.

  7. #7
    SitePoint Evangelist hantaah's Avatar
    Join Date
    Jul 2011
    Location
    Birmingham, Uk
    Posts
    562
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I've had a look at that and not only am I bad at maths but my php knowlege is next to nothing. I've tried all sorts of different variations and I'm not getting the desired result/ So now I have 10 worth of donations and a goal of 100.000 showing 19% done.

    here is my current setting:

    $percent = round(min(100000*($sum/$goal),100000));

    could someone let me know that correct way to do it?
    Much appreciation

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,873
    Mentioned
    208 Post(s)
    Tagged
    12 Thread(s)
    No problem:

    PHP Code:
    $have 10;
    $target 100000;
    $percentage = ( $have $target) * 100;
    $percentageToTwoDecimalPlaces round($percentage2);
    echo 
    "We have raised $have, out of $target.<br />";
    echo 
    "We have achieved $percentageToTwoDecimalPlaces% of our target"
    Hope that helps.

  9. #9
    SitePoint Evangelist hantaah's Avatar
    Join Date
    Jul 2011
    Location
    Birmingham, Uk
    Posts
    562
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I know your spelling this out for me but I'm still not getting it right:

    With your reply I changed it to this:

    Code:
    // Calculating how many percent of the goal were met:
    $have = 10;
    $target = 100000;
    $percentage = ( $have / $target) * 100;
    $percentageToTwoDecimalPlaces = round($percentage, 2);
    then further down the page I changed it to this:

    Code:
    <div class="donations">
        	<?php $percentageToTwoDecimalPlaces?>% done
        </div>
    the orinal was this:

    Code:
    // Calculating how many percent of the goal were met:
    $percent = round(min(100000*($sum/$goal),100000));
    and then:

    Code:
    iv class="donations">
        	<?php echo $percent?>% done
        </div>
    Can I not just change the sums in the $percent = round(min(100000*($sum/$goal),100000)); part?


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
  •