note 57048 added to function.round
| From: | blackhole at ggot dot org | Date: | Thu, 22 Sep 2005 12:01:18 +0000 |
| Subject: | note 57048 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95598@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57048
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57048&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57048&report=yes
Search -- http://master.php.net/manage/user-notes.php