I want a function that does
NearestFive(3) --> 5
NearestFive(5) --> 0
NearestFive(6) --> 5
if that makes sense. Best I can come up with is
function NearestFive ( x )
{
var r = x % 5;
if ( r == 0 ) return x;
else if ( r > 2 ) return x + 5 - r;
else return x - r;
}
but I'm wondering if it's possible to consolidate those steps or introduce bitshift operations to add efficiency?
For example, running two comparisons
r == 0
r > 2
may not be necessary since r > 2
implies r == 0
.