note 83150 deleted from function.round by nlopess
| From: | nlopess@php.net | Date: | Sun, 25 May 2008 09:03:16 +0000 |
| Subject: | note 83150 deleted from function.round by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139728@lists.php.net to get a copy of this message | ||
Note Submitter: marekfoo at gmail dot com
----
If you wish to round a number to the nearest X, here is a simple function:
<?php
function roundnum ($num, $nearest)
{
$ret = 0;
$mod = $num % $nearest;
if ($mod >= 0)
$ret = ( $mod > ( $nearest / 2)) ? $num + ( $nearest - $mod) : $num - $mod;
else
$ret = ( $mod > (-$nearest / 2)) ? $num - $mod : $num + ( -$nearest - $mod);
return $ret;
}
echo roundnum (1234, 15); // round to the nearest 15
echo roundnum (1234, 5); // round to the nearest 5
?>
more info at http://www.markblah.com/2008/05/11/php-round-number-to-nearest-x/