View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-19 #32 Old  
029 - Math.floor Alternative
 
You can use the bitwise right-shift operator to floor a number instead of using Math.floor(). Normally Math.floor() is absolutely fine to use, but if you are concerned about CPU cycles then this alternative will come in handy because it is a lot quicker than Math.floor().

Here is an example using Math.floor()...

ActionScript Code:
  1. var num:Number = 123.456;
  2. num = Math.floor(num);
  3.  
  4. trace( num ); // output: 123
  5.  
...and here is the same thing, but this time the bitwise right-shift operator is used...

ActionScript Code:
  1. var num:Number = 123.456;
  2. num >>= 0; // num >>= zero
  3.  
  4. trace( num ); // output: 123
  5.