This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The problem

Interviewer: Can you talk about some of the improvements es6 has made to numeric types?

Interviewer: Yes, the definition of binary and octal prefixes has been added, as well as numeric separators to make it more semantic. I also added some methods on Number.

Interviewer: Good, good, what methods on Number, can you list a few?

Interviewer: : I may not remember that very clearly.

The above scenario is purely personal fiction and may be similar.

Today we summarize what changes ES6 has made to numeric types.

Numeric types

Into the system

There are now fixed prefixes for binary, octal and hexadecimal numbers.

Binary 0b(or 0b), octal 0O (or 0O), hexadecimal 0x(or 0x).

console.log(0b11) / / 3
console.log(0o11) / / 9
console.log(0x11) / / 17
Copy the code

If you want to represent bases other than decimal, be sure to use a prefix.

The separator

Js used to have no delimiters, which makes it very difficult to read long numbers, such as:

12341243543554 

64352341243543  
Copy the code

Es now has a new numeric delimiter, represented by _.

12 _341_243_543_554  / / 12 trillion

64 _3523_4124_3543  / / 64 trillion
console.log(typeof 64 _3523_4124_3543) // number
console.log(64352341243543= = =64 _3523_4124_3543) // true
Copy the code

It’s a little bit more readable. It’s still a number

Using it has the following limitations:

  • You need numbers before and after
  • You just follow the decimal point
  • Scientific notation, e can’t be separated before or after it
  • Don’t inNumber.parseInt.parseFloatUse delimiters on functions
_123_ // error
1._23 // error
1e_123 // error
Number('123 _123) //NaN
parseInt('123 _123) / / 123
Copy the code

The new method

There were isFinite methods, isNaN methods, parseInt methods, and parseFloat methods globally. Now these methods are added to the Number object.

Number.isfinite (arg) : returns true if the parameter isFinite. If the argument is not a number, return false.

console.log(Number.isFinite(123)) // true
console.log(Number.isFinite(Infinity)) // false
console.log(Number.isFinite('123')) // false
console.log(Number.isFinite(true)) // false
Copy the code

Number.isNaN(arg) : Checks whether the argument is a NaN, and returns true if it is.

console.log(Number.isNaN(NaN)) // true
console.log(Number.isNaN(1)) // false
console.log(Number.isNaN('123')) // false
console.log(Number.isNaN(true)) // false
Copy the code

The traditional global method does not convert a parameter to number, but returns false if it is not a number.

Number.parseint (): Converts arguments to integers, discarding decimals if any are present.

console.log(Number.parseInt(1.4)) / / 1
console.log(Number.parseInt('1.6')) / / 1
console.log(Number.parseInt('aaa')) // NaN
Copy the code

Number.parsefloat (): Converts the argument to a floating point Number, keeping the decimal part if there is one.

console.log(Number.parseFloat(1.4)) / / 1.4
console.log(Number.parseFloat('1.6')) / / 1.6
console.log(Number.parseFloat('aaa')) // NaN
Copy the code

These two methods are exactly the same as the global behavior.

other

The following methods and properties have also been added:

Number.isinteger () : checks whether the parameter is an integer. But if the decimal part is.0, it’s also an integer. Non-numeric returns false.

Number.isInteger(1) // true
Number.isInteger(1.0) // true
Number.isInteger(1.1) // false
Number.isInteger(null) // false
Copy the code

Because js currently has a decimal maximum of 16 bits and a binary maximum of 53 bits, it will be ignored if it exceeds the number of bits.

// The 4 of the decimal point is converted to more than 53 bits and is ignored
console.log(3.000000000000000004) / / 3
console.log(Number.isInteger(3.000000000000000004)) // true
Copy the code

Number.EPSILON: represents the minimum difference between an integer and the smallest floating point of an integer in the safe integer range, that is, the minimum accuracy.

console.log(Number.EPSILON) / / 2.220446049250313 e-16
Number.EPSILON === Math.pow(2, -52) // true
Copy the code

Different from number. MIN_VALUE, it is the positive Number that represents the lowest accuracy of JS and is closest to 0.

Number.MIN_VALUE // 5e-324
Copy the code

Number.MAX_SAFE_INTEGER: indicates the maximum safe integer. Number.MIN_SAFE_INTEGER: indicates the minimum safe integer. Number.issafeinteger () : checks whether the parameter is a safe integer. Returns true if yes.

Number.MAX_SAFE_INTEGER / / 9007199254740991
Number.MIN_SAFE_INTEGER / / - 9007199254740991
Number.MAX_SAFE_INTEGER === Math.pow(2.53) - 1 // true

console.log(Number.isSafeInteger(9007199254740991)) // true
console.log(Number.isSafeInteger(9007199254740992)) // false
console.log(Number.isSafeInteger(-9007199254740991)) // true
console.log(Number.isSafeInteger(-9007199254740992)) // false
Copy the code

reference

Numerical extension