1. Floating point values

Floating-point values that must contain a target decimal point followed by at least one decimal point.

Var floatNum1 = 1.1; Var floatNum2 = 0.1; var floatNum3 = .1; // Valid, but not recommendedCopy the code

2. Floating point numerical calculation

Floating-point values have a maximum accuracy of 17 decimal places, but are far less accurate in arithmetic than integers. For example, 0.1 plus 0.2 is not 0.3, but 0.30000000000000004. This small rounding error makes it impossible to test specific floating point values.

If (a + b == 0.3){alert("You got 0.3."); // Do not do such tests! }Copy the code

3. Numerical range

The isFinite() function determines whether a value is between the maximum Number.MAX_VALUE and the minimum Number.MIN_VALUE, and returns true in this range.

var result = Number.MAX_VALUE + Number.MAX_VALUE; 
alert(isFinite(result)); //false 
Copy the code

4, NaN

NaN is not equal to any value, including NaN itself.

alert(NaN == NaN); //false 
Copy the code

IsNaN () function

This function takes an argument of any type, and the function helps us determine if the argument is “not a number.” After isNaN() receives > a value, it attempts to convert the value to a numeric value. Some non-numeric values are directly converted to numeric values, such as the string “10” or >Boolean values. Any value that cannot be converted to a value causes this function to return true.

alert(isNaN(NaN)); //true alert(isNaN(10)); //false (10 is a number) alert(isNaN("10")); //false (can be converted to value 10) alert(isNaN("blue")); //true (cannot be converted to value) alert(isNaN(true)); //false (can be converted to the value 1Copy the code

6. Numerical conversion

Number() can be used for any data type

  1. If Boolean, true and false are converted to 1 and 0, respectively.
  2. If it is a numeric value, it is simply passed in and returned.
  3. If it is null, 0 is returned.
  4. If undefined, NaN is returned.
  5. If it is a string, follow these rules:
  • If the string contains only numbers (including those preceded by a plus or minus sign), convert it to a decimal value, i.e., “1” becomes a 1, “123” becomes a 123, and “011” becomes an 11 (note: leading zeros are ignored);
  • If the string contains a valid floating-point format, such as “1.1”, it is converted to the corresponding floating-point value (again, leading zeros are ignored);
  • If the string contains a valid hexadecimal format, such as “0xf”, it is converted to a decimal integer value of the same size;
  • If the string is empty (containing no characters), it is converted to 0;
  • If the string contains characters other than those in the above format, it is converted to NaN.
  1. In the case of an object, the valueOf() method of the object is called and the returned value is converted according to the previous rules. If the result of the transformation is NaN, the object’s toString() method is called and the returned string value is converted again according to the previous rules.
var num1 = Number("Hello world!" ); //NaN var num2 = Number(""); //0 var num3 = Number("000011"); //11 var num4 = Number(true); / / 1Copy the code

The parseInt () function

  • If the first character is not a numeric character or a minus sign, parseInt() returns NaN;
  • Converting an empty string with parseInt() returns NaN (Number() returns 0 for empty characters)
  • If the first character is a numeric character, parseInt() continues parsing the second character until all subsequent characters are parsed or a non-numeric character is encountered. For example, “1234blue” is converted to 1234, and “blue” is ignored entirely. Similarly, “22.5” is converted to 22 because the decimal point is not a valid numeric character.
  • If the string begins with “0x” and is followed by a numeric character, it is treated as a hexadecimal integer; If a string begins with “0” and is followed by a numeric character, it is parsed as an octal number.
var num1 = parseInt("1234blue"); // 1234 var num2 = parseInt(""); // NaN var num3 = parseInt("0xA"); // 10 (hexadecimal number) var num4 = parseInt(22.5); // 22 var num5 = parseInt("070"); Var num6 = parseInt("70"); Var num7 = parseInt("0xf"); // 15 is a hexadecimal numberCopy the code
  • The parseInt() function can provide a second argument: the base (that is, how many bases) to use when converting.
var num1 = parseInt("10", 2); Var num2 = parseInt("10", 8); Var num3 = parseInt("10", 10); Var num4 = parseInt("10", 16); //16 (in hexadecimal formatCopy the code

parseFloat

  • ParseFloat () also parses each character from the first character (position 0). It also parses to the end of the string or until an invalid floating-point numeric character is encountered
  • The first decimal point in the string is valid, but the second decimal point is invalid, so the string after it is ignored.
  • Aside from the fact that the first decimal point is valid, the second difference between parseFloat() and parseInt() is that it always ignores leading zeros
  • ParseFloat () parses only decimal values, so it does not specify the use of the base with the second argument
var num1 = parseFloat("1234blue"); Var num2 = parseFloat("0xA"); //0 var num3 = parseFloat("22.5"); Var num4 = parseFloat("22.34.5"); Var num5 = parseFloat("0908.5"); Var num6 = parseFloat("3.125e7"); / / 31250000Copy the code