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.
($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:
echo round(($sum/$goal)*100, 2);
Hope that helps.