Js provides parseInt() and parseFloat() conversion functions. The former converts values to integers, and the latter converts values to floating-point numbers. These functions only work correctly if they are called on a String; All other types return NaN(Not a Number).

The parseInt() method also has a base mode that converts binary, octal, hexadecimal, or any other base string to an integer. The base is an integer specified by the second argument to the parseInt() method.

Another difference in using the parseFloat() method is that strings must represent floating-point numbers in decimal form, and parseFloat() has no base mode for floating-point numbers.

parseIntMethod takes two parameters:parseInt(string, radix?) ;parseFloatMethod takes a parameter:parseFloat(string); 
Copy the code

parseInt()

String: the value to be parsed. If the argument is not a string, it is converted to a string (toString). Whitespace at the beginning of the string is ignored.

Radix (optional) : radix from 2 to 36, representing the values being parsed. For example, specifying 10 is the same thing as specifying the decimal place.

1. Basic usage:

Only one parameter is accepted, which can be considered as the second parameter, which defaults to 10. The return value of parseInt can only be a decimal integer or a NaN.

A. Convert the string to an integer.

parseInt('123'); / / 123
Copy the code

B. If there is a space in the string header, the space is automatically removed.

parseInt('81'); / / 81
Copy the code

C. If the parseInt argument is not a string, it is converted to a string before conversion. This is important

D. When a string is converted to an integer, the string is converted one character at a time. If a character cannot be converted to a number, the string is stopped and the converted part is returned.

parseInt('99aa'); / / 99Copy the code

E. If the first character of the string cannot be converted to a number (except for a numeric sign), return NaN.

parseInt('aa99'); // NaN parseInt('-99'); / / - 99Copy the code

F. If a string starts with 0x or 0x, parseInt parses it as a hexadecimal number.

parseInt('0x10'); / / 16
Copy the code

G. If the string starts with 0, parse it in decimal.

parseInt('011') / / 11
Copy the code

H. If an argument starts with 0 but is not a string, the value is converted to a string and then parsed, as shown in rule C;

parseInt(011); / / 9
Copy the code

// To explain, (011).tostring () yields’ 9 ‘.

I. For numbers that are automatically converted to scientific notation, parseInt treats the scientific notation representation as a string, leading to some strange results.

The parseInt (1000000000000000000000.5); ParseInt ('1e+21'); / / 1 parseInt (0.0000008); // 8 // equivalent to parseInt('8e-7'); / / 8Copy the code

2. Base conversion (receiving two parameters) :

The parseInt method can also take a second argument (between 2 and 36) that represents the base number of the parsed value and returns the decimal number corresponding to that value. By default, parseInt’s second argument is 10, which means the default is decimal to decimal.

A. Refer to the first basic usage for the first parameter parsing rule

B. If the second parameter is not a value, it is automatically converted to an integer. The integer must be between 2 and 36 to give a meaningful result, beyond which NaN is returned. If the second argument is 0, undefined, and NULL, it is simply ignored.

The parseInt (" 19 ", 10); // 19 (10+9) parseInt(11, 2); // 3 (2+1) parseInt(" 17 ", 8); // 15 (8+7) parseInt(1f, 16); // 31 (16+15) parseInt('-99', null); // -99 parseInt('-99', undefined); // -99 parseInt('-99', 0); / / - 99Copy the code

The parseFloat ()

The usage is basically the same as above, without the second argument

parseFloat("1234blue"); / / returns 1234.0 parseFloat (" 0 xa "); / / returns NaN parseFloat (" 22.5 "); / / returns 22.5 parseFloat (" 22.34.5 "); / / returns 22.34 parseFloat (" 0908 "); //returns 908 parseFloat("blue"); //returns NaNCopy the code