-
Rounding
Folks,
When $_SESSION['sessionCounter'] is 9 and $_SESSION['limit'] is 8, I think that the PHP_ROUND_HALF_EVEN in
PHP Code:
$percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0), PHP_ROUND_HALF_EVEN);
should round $percentage to the even percentage, 12 percent. Instead, it is rounding up, to 13.
Am I missing something?
A.
-
There appears to be a typo in your code. Try this instead:
Code:
<?php
$_SESSION['sessionCounter']=9;
$_SESSION['limit']=8;
$percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0, PHP_ROUND_HALF_EVEN));
print $percentage;
?>
Note the difference in the placement of the parentheses.
-
James,
First of all, thank you!
I'll be happy to try it, unfortunately, it may be a few days before I am able to find the time to do so.
In comparing your statement against mine, the only semantic change I see is your adding a set of outer parentheses. Is there more that I missed? Regardless, I'll be happy to report results.
Thanks again.
A.
-
It is not very obvious, so here is your code and my code side by side. The parentheses highlighted in yellow was moved to the outside as seen in the second example where it is correct.
Code:
$percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0), PHP_ROUND_HALF_EVEN);
Code:
$percentage = (round(((($_SESSION['sessionCounter'] / $_SESSION['limit']) -1) * 100), 0, PHP_ROUND_HALF_EVEN));
Out of curiosity, what is the code used for?
-
James,
You have shown me the error of my ways. Thank you!
The code is used for messages to visitors to my "poker" page who are asking for more poker "hands" than their limit. It is a frivolous "hobby" page on (one of ) my professional web sites.