Using bitwise OR 0 to floor a number

前端 未结 6 1834
抹茶落季
抹茶落季 2020-11-22 05:01

A colleague of mine stumbled upon a method to floor float numbers using a bitwise or:

var a = 13.6 | 0; //a == 13

We were talking about it

6条回答
  •  独厮守ぢ
    2020-11-22 05:38

    • The specs say that it is converted to an integer:

      Let lnum be ToInt32(lval).

    • Performance: this has been tested at jsperf before.

    note: dead link to spec removed

提交回复
热议问题