note 43639 added to function.round
| From: | monte at ispi dot net | Date: | Mon, 28 Jun 2004 22:31:14 +0000 |
| Subject: | note 43639 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72078@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43639
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43639&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43639&report=yes
Search -- http://master.php.net/manage/user-notes.php