Very short Bitwise operation to do rounding similar to the equivalent ofMath.floor() i.e ~~5.9 // 5
It's handy for code golf or for angering coworkers
Edit: I didn't quite literally mean it's exactly the same as Math.floor I should've clarified on that. Also, I'm not suggesting anyone use this in production code, like I said it's nice for code golf.
Bitwise operators will convert Number which is 64-bit precision IEEE-754 to 32-bit signed integer. So you will lose precision if your number is over 2**32, and will get -ve value if your number is between 2**31 and 2**32. I think that's why Airbnb eslint rules forbidden bitwise operators.
Yeah, there are a few ways to do it. I was using num >> 0 before but ~~n is more concise and makes a little more sense (imo) like doing a double negation !!
40
u/a_dev_has_no_name Mar 19 '19 edited Mar 20 '19
~~anyNumberVery short Bitwise operation to do rounding similar to
the equivalent ofMath.floor()i.e~~5.9 // 5It's handy for code golf or for angering coworkers
Edit: I didn't quite literally mean it's exactly the same as
Math.floorI should've clarified on that. Also, I'm not suggesting anyone use this in production code, like I said it's nice for code golf.