Number

  1. The Number type uses 64-bit IEEE-754 format to represent integer and floating point values.

  2. Integers can be expressed in octal or hexadecimal, with octal’s first digit being 0 (0O in strict mode) and hexadecimal being 0x.

  3. If the number in an octal or hexadecimal literal is larger than it should be (for example, an 8 appears in octal), the preceding 0 is ignored and converted to decimal.

  4. Floating-point values use twice as much memory as integer values, so if there are no digits behind the decimal point, the value becomes an integer, or 0 after the decimal point.

    let num1=1.0;
    let num2=1.
    // They are treated as integers.
    Copy the code
  5. For extremely large values, you can use scientific notation, using upper or lower case e.

0.1 + 0.2 = 0.300… 4

The reason for this result is that an infinite loop occurs when the decimal part is converted to binary, so rounding is required.

Solutions:

  1. Multiply the decimal by a factor of N, and then shrink it by a factor of N.
  2. Just drop the trailing value.

The range of values

  1. Number.MIN_VALUE: the minimum
  2. Number.MAX_VALUEThe maximum value of:
  3. Out of bounds useinfinitysaid

NaN

NaN (not a number) : Not a number.

  1. Anything that involves a NaN results in a NaN
  2. NaN does not equal any value, including itself, i.eNaN==NaNforfalse
  3. useisNaN()Function to determine if it is NaN

Numerical transformation

Numbe() is a transformation function for any data type. ParseInt () and parseFloat() are used to convert strings to numeric values.

Number() function conversion rules:

  1. Boolean: true converts to 1, false converts to 0.

  2. Value: Returns directly.

  3. Null: Returns 0.

  4. Undefined: returns NaN.

  5. string

    • If the value contains valid decimal, hexadecimal, or floating point characters, the value is converted to the corresponding value.
    • An empty string that returns 0.
    • Characters other than the above, return NaN.
  6. Object: Call the ValueOf() method of the object and convert it according to the above rules. In the case of NaN, call the toString() method of the object and convert it according to the rules of strings.

ParseInt () :

  1. Receives the second digit, which specifies the base.
  2. Default is decimal

The parseFloat () :

  1. Only decimal values are parsed. Base cannot be specified.