2

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.

  1. Math.ceil() //Used for rounding up
  2. Math.floor() //Used for rounding down
  3. Math.round() //Round to whole number
  4. parseInt() //parse a string and return an integer
  5. parseFloat() //parses a string and returns a float
  6. toFixed() //Round to the specified number of decimal places
  7. toPrecision() //returns a numeric string of the specified length
  8. Number() // Convert the value of the object to a number
  9. 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.


沉静地闪光
11.2k 声望3.1k 粉丝

值得做的事情,值得现在就去做并一直坚持做;