I came here looking for something like that. If my number is -0, -1, -2, it should be -0, and if it is -3, -4, -5, then it must be greater than -5.
I came up with this solution:
function round(x) { return x%5<3 ? (x%5===0 ? x : Math.floor(x/5)*5) : Math.ceil(x/5)*5 }
And tests:
for (var x=40; x<51; x++) { console.log(x+"=>", x%5<3 ? (x%5===0 ? x : Math.floor(x/5)*5) : Math.ceil(x/5)*5) } // 40 => 40 // 41 => 40 // 42 => 40 // 43 => 45 // 44 => 45 // 45 => 45 // 46 => 45 // 47 => 45 // 48 => 50 // 49 => 50 // 50 => 50
AymKdn Sep 21 '17 at 8:34 on 2017-09-21 08:34
source share