在我以前的问题的接受的答案
( What is the fastest way to generate a random integer in javascript?),我想知道一个数字如何通过符号|失去其小数位
.
( What is the fastest way to generate a random integer in javascript?),我想知道一个数字如何通过符号|失去其小数位
.
例如:
var x = 5.12042; x = x|0;
那个楼层的数字是多少到5?
更多的例子:
console.log( 104.249834 | 0 ); //104 console.log( 9.999999 | 0 ); // 9
解决方法
因为根据ECMAScript规范,按位运算符操作符调用ToInt32对每个表达式进行评估.
见11.10 Binary Bitwise Operators:
The production
A : A @B
,where@
is one of the bitwise operators in
the productions above,is evaluated as follows:
Evaluate
A
.Call
GetValue(Result(1))
.Evaluate
B
.Call
GetValue(Result(3))
.Call
ToInt32(Result(2)).
Call
ToInt32(Result(4)).
Apply the bitwise operator
@
toResult(5)
andResult(6)
. The result is a signed 32 bit integer.Return
Result(7)
.