Created
May 3, 2011 09:56
-
-
Save danielgwood/953089 to your computer and use it in GitHub Desktop.
Round a decimal to the nearest half
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 | |
/** | |
* 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