note 43639 rejected from function.round by joey
| From: | joey@php.net | Date: | Thu, 04 Dec 2008 02:56:50 +0000 |
| Subject: | note 43639 rejected from function.round by joey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147605@lists.php.net to get a copy of this message | ||
Note Submitter: monte at ispi dot net
----
Just a quick note on rounding to the nearest "half" instead of nearest "whole".
I my case, I'm showing a user rating with stars and need to show a star for each whole, and
half a star for remainder values closer to half than whole.
// replace 3.45 with any rating
$_actual_rating = 3.45;
$_rating_round_half = round($_actual_rating / 0.5) * 0.5;
$_remainder = fmod($_rating_round_half, 1);
$_rating = (int) $_rating_round_half - $_remainder;
$_half_star = $_remainder == 0.5;
In the above example, $_rating will contain a whole number for the number of stars to display, and
$_half_star will be a boolean to determine whether or not to display half a star as well.