Make a summary every day, persistence is victory!
/** @date 2021-06-22 @description Number */Copy the code
One (sequence)
Number is a basic JS data type used to represent numbers. There are no Int and Float types in JS, all are represented by Nunber.
MAX_SAFE_INTEGER specifies the maximum Number of safe digits. ES2020 uses BigInt, which can be used to represent higher integers.
Number can be declared in two ways:
- literal
const num = 18;
Copy the code
- Constructor that uses the valueOf method to get the original value
const numObj = new Number(18); const num = numObj.valueOf(); / / 18Copy the code
Ii (Common Methods)
Common code:
const num = 18, str = '18';
Copy the code
- IsFinite: Determines whether a finite Number is present. Note that globalThis also has an isFinite method, but the global method implicitly converts to number. isFinite
globalThis.isFinite(str); // true
Number.isFinite(str); // false
Number.isFinite(num); // true
Number.isFinite(NaN); // false
Number.isFinite(Infinity); // false
Number.isFinite(2 ** 53); // true
Copy the code
- IsInteger: checks whether the incoming value is an integer
Number.isInteger(18); / / true Number. IsInteger (18.0); / / true Number. IsInteger (18.1); // false // polyfill function isInteger(val) { return ( typeof val === 'number' && Number.isFinite(val) && Math.round(val) === val ); }Copy the code
- IsNaN: Determine if the value passed in is a NaN. Note that there is also an isNaN globally, but that global methods are implicitly converted
Number.isNaN('NaN'); // false
globalThis.isNaN('NaN'); // true
Number.isNaN(undefined); // false
globalThis.isNaN(undefined); // true
Number.isNaN({}); // false
globalThis.isNaN({}); // true
Number.isNaN(NaN); // true
Number.isNaN(1 / 0); // false
Number.isNaN(0 / 0); // false
Copy the code
- IsSafeInterger: Checks whether the incoming value is a safe integer, interval
[-(2 ** 53-1), 2 ** 53-1]
Is a safe integer
Number.isSafeInteger(2 ** 53 - 1); // true
Number.isSafeInteger(2 ** 53); // false
Number.isSafeInteger(-(2 ** 53 - 1)); // true
Number.isSafeInteger(-(2 ** 53)); // false
Number.isSafeInteger(NaN); // false
Number.isSafeInteger(Infinity); // false
Copy the code
- ParseFloat: Like global parseFloat, converts the passed value to a floating point number. Note that special strings such as 1.23test and 1.23.45 take the first part of the conversion and convert it to a String if the passed value is not a String
Number. The parseFloat (' 1.23 '); / / 1.23 Number. ParseFloat (' 1.23 test '); / / 1.23 Number. ParseFloat (' 1.23.34 '); / / 1.23 Number. ParseFloat ([1.23]); / / 1.23 Number. ParseFloat ([1.23, 2.34]); / / 1.23 Number. ParseFloat (1.23} {num:); // NaN Number.parseFloat('2e3'); // 2000 Number.parseFloat('2e3n'); / / 2000Copy the code
- ParseInt: Like the global parseInt method, you can pass in a string that needs to be converted to a number of bases (optional, between 2 and 36, NaN is returned if the value exceeds 10) in addition to the value to be converted, and will not handle scientific notation
Number. The parseInt (' 1.23 '); / / 1 Number. The parseInt (' 1.23 test); / / 1 Number. The parseInt (' 1.23.34 '); / / 1 Number. The parseInt ([1.23]); / / 1 Number. ParseInt ([1.23, 2.34]); / / 1 Number. ParseInt (1.23} {num:); // NaN Number.parseInt('2e3'); // 2 Number.parseInt('2e3n'); // 2 Number.parseInt(2e3); // 2000 Number.parseInt('10', 37); // NaNCopy the code
About the second parameter radix does not pass or pass undefined or 0 there are the following three cases:
A. If a string begins with '0x' or '0x', hexadecimal conversion is performed number.parseint ('0X10'); // 16 Number.parseInt('0X10', 0); // 16 Number.parseInt('0X10', 10); // 0 b. The string starts with '0'. Some browsers convert the string to base 8, and some browsers convert the string to base 10. In other cases, the conversion is done in base 10Copy the code
- ToFixed: Converts a number to a decimal point. Returns a string with the number of decimal points. The default value is 0, and the range is mostly [0-100]
num.toFixed(); Num.tofixed (2); // '18' // num.tofixed (2); // '18.00' // If the number of decimal places is sufficient, it will be rounded based on the argument passed in (18.1234). / / '18.12' (18.1256). ToFixed (2); // '18.13' // passing floating point number (18.1256). ToFixed (2.634); / / '18.13'Copy the code
- ToPrecision: Returns a string with the specified precision in the range of [1-100]. If it is not passed, it is similar to toString
num.toPrecision(); Num.toprecision (4); // num.toprecision (4); Num.toprecision (1); num.toprecision (1); // '2e+1' // if the number of digits is sufficient, it will be rounded according to the parameters passed in (18.1234). / / '18.1' (18.1534). The toPrecision (3); // '18.2' // passing float (18.1256). ToPrecision (2.634); / / '18'Copy the code
- ToString: Returns a string. See the summary of methods
- ToValue: Returns the original value, mainly used to return the original value of the Number object
const numObj = new Number(18); const numValue = numObj.valueOf(); / / 18Copy the code