note 57048 deleted from function.round by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 16:22:52 +0000 |
| Subject: | note 57048 deleted from function.round by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154811@lists.php.net to get a copy of this message | ||
Note Submitter: blackhole at ggot dot org
----
Sometimes you will want to make a number divisible by another (like rounding by 5s, or 10s, or 7s,
or 1438s, or whatever) instead of just making it divisble by one such as round() with only one
argument. The function that follows is fairly simply and behaves mostly as expected.
<?php
function divby($val, $by) {
if ($val < 0)
$val += -$val % $by;
else
$val -= $val % $by;
return $val;
}
?>
divby(23, 10) will return 20
divby(77, 5) will return 75
divby(138, 7) will reuturn 133
divby(-23, 10) will return 20
note: the only way to return a negative value is to make BOTH values negative, for example:
divby(-25, -10) will return -20
note also: if the value of $by is greater than $val then the function will return 0, for example:
divby(3, 4) will return 0
divby(4, 3) will return 3