Programming tricks related to ActionScript, Python, C/C++ and Java. See the comments for answers.
Friday, January 15, 2010
AS3: Remove fractional part in Number
Give a one line expression to remove the fraction part from a Number variable in AS3. Hint: should work for positive as well as negative numbers, and more than 32-bit numbers.
value = (value < 0 ? Math.ceil(value) : Math.floor(value))
Note that the other solutions of (1) typecast to int() does not work for more than 32-bit numbers, because int is 32-bits whereas Number can be larger in AS3, and (2) Math.floor(value) doesn't work for negative numbers because floor(-4.3) == -5.
1 comment:
value = (value < 0 ? Math.ceil(value) : Math.floor(value))
Note that the other solutions of (1) typecast to int() does not work for more than 32-bit numbers, because int is 32-bits whereas Number can be larger in AS3, and (2) Math.floor(value) doesn't work for negative numbers because floor(-4.3) == -5.
Post a Comment