note 34823 added to function.round
| From: | ianring at rack1 dot php dot net | Date: | Thu, 07 Aug 2003 17:30:11 +0000 |
| Subject: | note 34823 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-53589@lists.php.net to get a copy of this message | ||
The round() function may indeed work properly with half-values (eg. 1.5), but this little method
will give you peace of mind. Add some "fuzz" to your function with a miniscule delta
value.
$delta = 0.00001;
$x = round($x+$delta);
This is fine, unless $x has a value of 1.49999 ... if you worried about that, use this method
instead:
if(($x-floor($x))==0.5){
$x+=$delta;
}
$x = round($x);
you can change your "optimistic" delta into a "pessimistic" delta by subtracting
instead of adding.
Cheers,
Ian Ring
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34823
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34823&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34823&report=yes