Last active
September 25, 2015 04:51
-
-
Save benjamw/0555bb753124688b5bee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** function my_round | |
* | |
* rounds a value to the closest value | |
* starting at another value | |
* e.g.- my_round(6.28318, 5, 2) => 7 | |
* | |
* To round to closest of: | |
* 2 6 10 14 18 22 ... | |
* | |
* Each value is 4 more than the previous | |
* and the values start at 2 | |
* So round to the closest 4 starting at 2 | |
* my_round($val, 4, 2); | |
* | |
* @param float/int the given value | |
* @param int optional the closest value to round to (must be gte +1) | |
* @param int optional the starting value to round to (any integer) | |
* @return int the rounded value | |
*/ | |
function my_round($value, $closest = 1, $starting = 0) { | |
$closest = (int) $closest; | |
$starting = (int) $starting; | |
if (1 > $closest) { | |
$closest = 1; | |
} | |
return (int) ((((int) round(($value - $starting) / $closest)) * $closest) + $starting); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment