Skip to content

Instantly share code, notes, and snippets.

@benjamw
Last active September 25, 2015 04:51
Show Gist options
  • Save benjamw/0555bb753124688b5bee to your computer and use it in GitHub Desktop.
Save benjamw/0555bb753124688b5bee to your computer and use it in GitHub Desktop.
<?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