Created
December 8, 2012 15:23
-
-
Save mircobabini/4240737 to your computer and use it in GitHub Desktop.
rounds a float to the nearest on step-scale
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
<? | |
/** | |
* rounds a float to the nearest on step-scale | |
* | |
* @param float $value value to round | |
* @param float $step step-scale, | |
* @return float rounded value | |
* | |
* @package Handframe/Tool/Function | |
* @author Mirco Babini <[email protected]> | |
* @license http://creativecommons.org/licenses/by-nc/3.0/ | |
*/ | |
function round_step ($value, $step = 1) | |
{ | |
$mul = round (1 / $step); | |
return (round ($value * $mul)) / $mul; | |
} | |
// examples: | |
// round_step (2.5, 1); => 3 | |
// round_step (2.5, 0.5); => 2.5 | |
// round_step (2.6, 0.5); => 2.5 | |
// round_step (2.5, 0.3); => 2.6666666666667 | |
// round_step (2.45, 0.2); => 2.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment