JS operator single vertical bar "|" The role
In the JS integer operation, the equivalent of removing the decimal point, parseint. When a positive number is equivalent to Math.floor (), a negative number is equivalent to the Math.ceil () Note:
1. Math.ceil () is used for rounding up.
2. Math.floor () is used as a downward rounding.
3. Math.Round () The rounded rounding we used in maths.
Console.log (0.6|0)//0
console.log (1.1|0)//1 console.log
(3.65555|0)//3 console.log
(5.99999|0)//5
Console.log ( -7.777|0)//-7
Note: We often use parseint (), parsefloat (), toFixed () and toprecision () and so on, in addition to the math's three methods for handling numbers. Simple explanation:
The Tofixed method uses the following:
100.456001.toFixed (2); 100.47
100.456001.toFixed (3);//100.456
Number.prototype.toFixed.call (100.456001,2);//100.47
Disadvantage: After use, it will become a string.
Toprecision usage is as follows:
99.456001.toPrecision (5); 99.456
100.456001.toPrecision (5);//100.46
Number.prototype.toPrecision.call (10.456001,5);//10.456
Rule of operation of single vertical bar
Look at the above example, the general know that a single vertical bar can be used to take the whole operation, that is, only the positive part, the decimal part by taking off, but "|", and how to operate it, why can "|" to achieve the purpose of rounding? The single vertical bar is not 0 What is the number?
With these questions, let's look at the following examples:
Console.log (3|4); 7
Console.log (4|4);//4 Console.log (8|3);//11 Console.log (5.3|4.1);//5 console.log
(9|3455);/ 3455
All right, I'm here to announce the answer. In fact, single vertical bar "|" Is the result of the addition of the conversion to the 2 binary. For example, let's take a simple example:
Copy Code code as follows:
After converting to binary 011|100 add to 111=7
Copy Code code as follows:
After converting to binary 100 |100 add to get 100=4
Copy Code code as follows:
After converting to binary 1000 |011 add to get 1011=11