Re: Rounding
| From: | Ernest E Vogelsinger | Date: | Thu, 20 Feb 2003 21:28:44 +0000 |
| Subject: | Re: Rounding | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-136420@lists.php.net to get a copy of this message | ||
At 22:17 20.02.2003, Van Andel, Robbert spoke out and said:
--------------------[snip]--------------------
>How do I round a number to the nearest 10 or even 5. Say I have a
>number like 12. Is there an easy way to round that up to 15 or 20?
--------------------[snip]--------------------
function round_to($number, $full, $isup=true)
{
$factor = $number % $full;
if ($isup) {
if ($factor) $factor = $full - $factor;
$result = $number + ($factor ? $factor : 0);
}
else
$result = $number - $factor;
return $result;
}
$x = 12;
$to = 5;
echo "$x => ", round_to($x, $to), "\n", "$x => ", round_to($x,
$to, false);
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/