JavaScript - rounding

There are six common operations for rounding numbers in JavaScript:

  1. ceil --improved arrangement
  2. floor - round down
  3. round - round up
  4. toFixed - fixed precision
  5. toPrecision - fixed length
  6. parseInt - rounding (bit operators)

1, improvement arrangement: ceil

ceilYes 天花板means the integer above and closest to a number. ceil is a static method of the Math object. It needs to pass a parameter. The calling method is as follows:

Math.ceil(12.34); //13
Math.ceil(12.68); //13

2. Round down: floor

floorYes 地板means that it is the integer below a number and the nearest integer to that number. floor is a static method of the Math object. It needs to pass a parameter. Its calling method is as follows:

Math.floor(12.34); //12
Math.floor(12.68); //12

3. Rounding up: round

roundThe effect of is to round a floating-point number, preserving the integer bits. round is also a static method of the Math object, and also needs to pass a parameter. The calling method is as follows:

Math.round(12.34); //12
Math.round(12.54); //13

4. Fixed precision: toFixed

toFixedDifferent from the above three methods, it is a method implemented on the Number prototype, and its function is to round a floating point number and retain a fixed number of decimal places. toFixed needs to be passed a parameter, which is called as follows:

100.456001.toFixed(2); //100.46
100.456001.toFixed(3); //100.456

5. Fixed length: toPrecision

toPrecisonIt is also a method implemented on the Number prototype to deal with floating-point numbers. Unlike toFixed, it rounds a floating-point number and retains fixed-length significant digits, including the integer part.

99.456001.toPrecision(5);  //99.456
100.456001.toPrecision(5); //100.46

6. Rounding: parseInt (bit operator)

parseIntis a method on the global object window, its function is to round a convertible value, which is divided into the following two cases:

  • Convert the string value to a Number integer, and convert each character of the string until it encounters an unconvertible character (including the decimal point), and stop Rounding
    the floating-point type value, ignoring the decimal part, and not doing rounding processing
//字符串数值
parseInt('100') ; // 100
parseInt('100axt'); //100
parseInt('100xh20'); //100
parseInt('100.78'); //123
//Number 类型
parseInt(100.12) ; // 100
parseInt(100.78); //100
  • In addition to rounding through parseInt, rounding can also be achieved through some bit operators:
| 0   : 和 0 进行 按位或 操作,原值不变
~~   : 两次 按位非 操作得到的也是原值
>> 0 : 右移 0
<< 0 : 左移 0
>>> 0: 无符号右移 0

These bitwise operators exhibit some common characteristics when implementing rounding operations:

For the Number type, applying the bit operation directly will get almost the same result as parseInt;
for other types, it will be converted to a numeric value through Number() internally, and then the bit operation will be applied.
When applying bit operations to the special NaN and Infinity values, both values ​​are treated as 0.

//Number 类型:直接应用位操作
~~ 100.12;  //  100
100.78  |  0;    //  100
100.45 >>  0;    //  100
100.50 <<  0;   // 100
100.96 >>> 0;    //  100

//其他类型:先使用 Number() 转换为数值,再应用位操作
~~ '100.12'  // 100, Number('100.12') == 100.12
'100.50'  >> 0;  // 100,Number('100.50') == 100.50
'100.96'  << 0;  // 100,Number('100.96') == 100.96
~~ 'abc'  // 0 , Number('abc') == NaN
'12abc'  >> 0;  // 0, Number('12abc') == NaN
undefined | 0 ; // 0, Number(undefined) == NaN
~~null;      // 0 , Number(null) == 0
true >> 0; // 1 , Number(true) == 1
false >> 0; //0 , Number(false) == 0
[]  << 0;     // 0 , Number([]) == 0
~~NaN; // 0
Infinity >>> 0; // 0

Bitwise operators are used to manipulate values ​​at the most basic level, by the bits in memory that represent the value. All values ​​in ECMAScript are stored in IEEE-754 64-bit format, but bitwise operators do not directly manipulate 64-bit values. Instead, convert the 64-bit value to a 32-bit integer, perform the operation, and finally convert the result back to 64-bit. This is why bitwise operators can implement rounding.

In view of this implementation mechanism, bit operators in JavaScript can only support 32-bit numbers. Numbers larger than 32 bits will have overflow problems during low-level conversion, and an additional layer of conversion will also reduce the efficiency of bit operations. But despite this, the bitwise operators are still much faster than parseInt when rounding numbers, and the performance is significantly improved.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325708853&siteId=291194637