Skip to content

Instantly share code, notes, and snippets.

@mircobabini
Created December 8, 2012 15:23
Show Gist options
  • Save mircobabini/4240737 to your computer and use it in GitHub Desktop.
Save mircobabini/4240737 to your computer and use it in GitHub Desktop.
rounds a float to the nearest on step-scale
<?
/**
* 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