This can be done in several ways, depending on your preferred rounding agreement:
1. Round to the next multiple of 5, delete the current number
Behavior: 50 outputs 55, 52 outputs 55
function roundUpToAny($n,$x=5) { return round(($n+$x/2)/$x)*$x; }
2. Round to the nearest multiple of 5 includes the current number
Behavior: 50 outputs 50, 52 outputs 55, 50.25 outputs 50
function roundUpToAny($n,$x=5) { return (round($n)%$x === 0) ? round($n) : round(($n+$x/2)/$x)*$x; }
3. Round to an integer, then to the nearest multiple of 5
Behavior: 50 outputs 50, 52 outputs 55, 50.25 outputs 55
function roundUpToAny($n,$x=5) { return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x; }
SW4 Nov 09 '10 at 13:00 2010-11-09 13:00
source share