用途:
双波浪线(~~):可以去除一个数字的小数部分,得到一个整数。
举例:
~~1 // 执行结果:1
~~1.2 // 执行结果:1
~~1.8 // 执行结果:1 不会四舍五入
~~-1 // 执行结果:-1
~~-1.2 // 执行结果:-1
~~0 // 执行结果: 0
注意:
Math.floor也可以去除一个数字的小数部分,得到一个整数,但由于Math.floor是向下取整的,所以Math.floor和~~在对带有小数点的负数进行操作时执行结果会不一样。
举例:
~~(1.2) // 执行结果:1
Math.floor(1.2) // 执行结果: 1
~~(-1.2) // 执行结果:-1
Math.floor(-1.2) // 执行结果: -2