# Calculations

• Feb 22, 2013, 13:48
hantaah
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?
• Feb 22, 2013, 14:12
felgall
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.
• Feb 22, 2013, 14:28
Pullo
Maybe the op means 100k?
(In which case 100.000 should be written without the decimal point).
• Feb 22, 2013, 16:38
carpekd
Quote:

Originally Posted by Pullo
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
• Feb 23, 2013, 07:14
hantaah
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));```
• Feb 23, 2013, 08:20
Pullo
Hi,

Have a look at this: http://www.phpbook.net/how-to-calcul...-with-php.html
That should helpwith what you want to do.
• Feb 26, 2013, 15:05
hantaah
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
• Feb 26, 2013, 15:18
Pullo
No problem:

PHP Code:

``` \$have = 10; \$target = 100000; \$percentage = ( \$have / \$target) * 100; \$percentageToTwoDecimalPlaces = round(\$percentage, 2); echo "We have raised £\$have, out of £\$target.<br />"; echo "We have achieved \$percentageToTwoDecimalPlaces% of our target";  ```
Hope that helps.
• Feb 26, 2013, 15:39
hantaah
I know your spelling this out for me but I'm still not getting it right:

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>```

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?
• Feb 27, 2013, 00:34
Pullo
Quote:

Originally Posted by hantaah
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);  ```
• Feb 27, 2013, 09:38
hantaah
Quote:

Originally Posted by Pullo
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

• Feb 27, 2013, 13:29
Pullo
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)*100, 2);  ```
Hope that helps.
• Feb 28, 2013, 15:00
hantaah
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