SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Calculations

  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,868
    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
    6,045
    Mentioned
    219 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
    6,045
    Mentioned
    219 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
    6,045
    Mentioned
    219 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?

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,045
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by hantaah View Post
    Can I not just change the sums in the $percent = round(min(100000*($sum/$goal),100000)); part?
    Well, that would be:

    PHP Code:
    $percent round((($sum/$goal)*100), 2); 

  11. #11
    SitePoint Evangelist hantaah's Avatar
    Join Date
    Jul 2011
    Location
    Birmingham, Uk
    Posts
    562
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Well, that would be:

    PHP Code:
    $percent round((($sum/$goal)*100), 2); 
    That did it!!! Having second thoughts about learning php, not sure if I will be able to grasp it

    many thanks for your help

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

    First off, I'm glad that helped.

    Secondly, don't give up on the PHP just yet.
    I know it might seem complicated, but let me explain:

    We can break our statement down into various parts.

    PHP Code:
    ($sum/$goal)*100 
    This is how we calculate the percentage.
    Divide the goal by the sum, then multiply by 100.
    We don't really need the brackets, but I find it makes it easier to read, as the division is done first, then the multiplication.

    Now we want to make sure that our answer is acurate to two decimal places.
    In this case, we don't have to do anything else, as 10 divided by 100,000 is 0.01.
    However, this won't always be the case (e.g. 100 divided by 100,000 is 0.1).
    To make sure that a number is rounded up/down to two decimal places we use the round() function.

    The syntax is like this: round(numberToRound, numberOfDecimalPlaces)

    e.g. round(3.14159265359, 2) will yield 3.14

    So, we want to do this: round(ourPercentage, 2).

    And this equates to round(($sum/$goal)*100, 2).

    Then you can just output this to the screen:

    PHP Code:
    echo round(($sum/$goal)*1002); 
    Hope that helps.

  13. #13
    SitePoint Evangelist hantaah's Avatar
    Join Date
    Jul 2011
    Location
    Birmingham, Uk
    Posts
    562
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ahhhh I seeeee!!! It was all going right over my head but now it makes sense. I'm not one to give up easily so I will plod on but my current course is flying through code. I feel I learn better by example, I guess I will just have to take my time, I think html and css seemed like that too when I first layed eyes on it.

    Thanks very much for your time, help and explanations


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
  •