Flash Cookbook - Appendix - Rounding
There came a big question with my brilliant collegue (Richárd Nagy 'Kapa' programmer). How to write a function what rounds our integer to the closest number, dividable by 5.
Of course as true and tricky (tt) programmers we immediately have to think about dividable by any given k integer.
Let' see our beauty:
var n:Number = 24;
var k:Number = 7;
n = n % k == 0 ? n : n % k >= k / 2 ? n - (n % k) + k : n - (n % k);
trace(n);
Note % means modulo and the structure
m = m == 0 ? 0 : 1
can be writtean as
if(m == 0) m = 0 else m = 1
The given numbers above (n=24, k=7) give the result 21.