Skip to content

Instantly share code, notes, and snippets.

@AlexPashley
Created July 3, 2013 21:56
Show Gist options
  • Save AlexPashley/5923211 to your computer and use it in GitHub Desktop.
Save AlexPashley/5923211 to your computer and use it in GitHub Desktop.
PHP: Round to nearest multiple with PHP
/**
* 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