note 102848 deleted from function.floor by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 Mar 2011 15:20:16 +0000 |
| Subject: | note 102848 deleted from function.floor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177754@lists.php.net to get a copy of this message | ||
Note Submitter: Joe T.
----
Yet another answer: why make a second function call when you can use basic math operators (or is
that what operators secretly do)?
function floor_x($number, $x){
return $number - $number % $x;
}
floor_x(250, 100);
// 200
floor_x(23, 20);
// 20
floor_x(32, 100);
// 0
The modulus gives you the remainder to subtract from the original number. In ex.1, the remainder is
50. In ex.2, it's 3. And in ex.4 it's 32 because 32 / 100 = 0, rem 32.