Created
July 3, 2013 21:56
-
-
Save AlexPashley/5923211 to your computer and use it in GitHub Desktop.
PHP: Round to nearest multiple with PHP
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
/** | |
* Round up to the nearest multiple of your choice. | |
* | |
* @param int $number | |
* @param int $near | |
* @return int | |
*/ | |
function get_nearest_multiple( $number, $near ) | |
{ | |
$nearest = round($number/$near)*$near; | |
return $nearest; | |
} | |
// examples | |
echo get_nearest_multiple(4.7,0.5); // prints 4.5 | |
echo get_nearest_multiple(4.2,0.5); // prints 4 | |
echo get_nearest_multiple(3.9,0.5); // prints 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment