Skip to content

Instantly share code, notes, and snippets.

@danielgwood
Created May 3, 2011 09:56
Show Gist options
  • Save danielgwood/953089 to your computer and use it in GitHub Desktop.
Save danielgwood/953089 to your computer and use it in GitHub Desktop.
Round a decimal to the nearest half
<?php
/**
* Round a decimal to the nearest half, so:
* 0 becomes 0
* 0.2 becomes 0
* 1 becomes 1
* 2.5 becomes 2.5
* 2.51 becomes 2.5
* 2.49 becomes 2.5
* 4.9 becomes 5
*
* @param float $decimal
* @return float
*/
function roundToHalf($decimal) {
return round($decimal / 0.5) * 0.5;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment