The role of the three:

Number(): can be used to convert any data type to a numeric value;

ParseInt (), parseFloat(): used specifically to convert strings to numbers;

A, Number (s) :

  • (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, return 0.
  • (4) If undefined, return NaN.
  • (5) If it is a string, follow the following rules: If the string is not a purely numeric string after truncating the leading and trailing whitespace, the result is NaN. If the string contains only numbers (including those preceded by a plus or minus sign), convert it to a decimal value, that is, “1” becomes a 1, “123” becomes a 123, and “011” becomes an 11 (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”, the rest is converted to decimal integer values 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, the rest is converted to NaN.
  • (6) If it is 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 toString() method of the object 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);           //1

var num5=Number("num123")       //NaN
Copy the code

ParseInt () :

The parseInt() function converts a string to an integer. In contrast to the Number() function, parseInt() can parse not only pure numeric strings, but also partial numeric strings that begin with a Number(non-numeric strings are removed during conversion).

  • (1) parseInt() returns NaN if the first character is not a numeric character or a minus sign; That is, converting an empty string with parseInt() returns NaN.
  • (2) 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.
  • (3) If the string begins with “0x” followed by a numeric character, it is treated as a hexadecimal integer.
  • (4) If the string begins with a “0” followed by a numeric character, it is treated as an octal integer.
  • (5) The parseInt() function adds a second argument to specify the base to use when converting.
  • (6) When parseInt() parses a floating point string, the round down method is used.
var num1=parseInt("num123");    //NaN
var num2=parseInt("");          //NaN
var num3=parseInt("123.45")     //123
var num4=parseInt("101010",2)   //42
var num5=parseInt("123num")     //123
var num6=parseInt("0xff") / / 255Copy the code

Third, the parseFloat () : with the parseInt (), the parseFloat () can also be resolved to digital part of the beginning of the string (not digital part of the string will be removed in the process of transformation). Unlike parseInt(), parseFloat() converts strings to floating point numbers; At the same time, however, parseFloat() takes only one argument and can only handle decimal strings.

  • (1) The first decimal point in the string is valid, while the second decimal point is invalid, so the string after it is ignored.
  • (2) parseFloat() returns an integer if the string contains a number that can be parsed as an integer (without a decimal point, or all zeros after the decimal point).
var num1=parseFloat("1234blue");    //1234

var num2=parseFloat("0xA");         //0

var num3=parseFloat("0908.5"); / / var num4 = parseFloat (908.5"3.125 e7");     //31250000

var num5=parseFloat("123.45.67") / / var num6 = parseFloat (123.45"")             //NaN

var num7=parseFloat("num123")       //NaN
Copy the code