Many people are very familiar with the double vertical bar "||", because this is often used in projects. The single vertical bar "|" is rarely used in project development. "|" is a bitwise operator and "||" is a logical operator.
Usually, the following methods are often used to process numbers.
- Math.ceil() //Used for rounding up
- Math.floor() //Used for rounding down
- Math.round() //Round to whole number
- parseInt() //parse a string and return an integer
- parseFloat() //parses a string and returns a float
- toFixed() //Round to the specified number of decimal places
- toPrecision() //returns a numeric string of the specified length
- Number() // Convert the value of the object to a number
- isFinite() //Check if a value is an infinite number
In fact, "|" can also perform simple rounding on numbers.
console.log(0.1|0)//0
console.log(1.1|0)//1
console.log(2.345|0)//2
console.log(3.99999|0)//3
console.log(-4.567|0)//-4
From the print, it can be seen that the "|" single vertical bar can round up numbers, that is, only the integer part is retained. | 0 can be rounded down, because bitwise operators work on 32-bit numbers, and any number operations will be converted to 32-bit. The binary of 0 is 00...0 with a total of 32 bits (32 0s), and no matter whether any number is ORed with 0, it is the original number. An integer "|" 0 can be obtained by itself, and a decimal bitwise OR with 0 can be rounded.
console.log(3|4); //7
console.log(4|4);//4
console.log(8|3);//11
console.log(5.3|4.1);//5
console.log(5.3|1688);//1693
The single vertical bar "|" here is the result of adding and printing the two numbers after the decimal number is converted to binary.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。