The differences between Number (), parseInt (), and parseFloat () in js are described in detail.
I. Number ()
If it is a Boolean value, the true and false values are converted to 1 and 0 respectively.
If it is a numeric value, it is just a simple input and return.
If the value is null, 0 is returned.
If it is undefined, NaN is returned.
If it is a string:
A. If the string contains only numbers, convert it to a decimal value. The leading 0 is ignored.
B. If the string contains a valid floating point format, such as "1.1", convert it to the corresponding floating point number, ignoring the leading 0
C. If the string contains a valid hexadecimal format, such as "0xf", convert it to a decimal value of the same size.
D. If the string is empty, convert it to 0.
E. If the string contains characters other than the preceding format, convert it to NaN
If it is an object, call the valueOf () method of the object and convert the returned value according to the previous rules. If the conversion result is NaN, call the toString () method of the object and convert the returned string value according to the preceding rules.
Example:
var num1 = Number("Hello world"); //NaN var num2 = Number(""); //0 var num3 = Number("0000011"); //11
Ii. parseInt ()
ParseInt () is more commonly used when processing integers. When converting a string, the parseInt () function ignores spaces in front of the string and finds the first non-space character.
If the first character is not a number or a negative number, parseInt () returns NaN. Similarly, if parseInt () is used to convert null strings, NaN is returned.
If the first character is a numeric character, parseInt () will continue to parse the second character until all subsequent strings are parsed or a non-numeric character is encountered.
The parseInt () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer.
The base is specified by the second parameter of the parseInt () method, so the hexadecimal value must be parsed. Of course, for binary, octal, or even decimal (default mode ), you can call the parseInt () method in this way.
Example:
Var num1 = parseInt ("AF", 16); // 175 var num2 = parseInt ("AF"); // NaN var num3 = parseInt ("10", 2 ); // 2 (based on binary parsing) var num4 = parseInt ("sdasdad"); // NaN
Iii. parseFloat ()
Similar to the parseInt () function, parseFloat () parses each character from the first character (position 0. It is also resolved to the end of the string, or to an invalid floating point numeric character.
That is to say, the first decimal point in the string is valid, and the second decimal point is invalid, and the strings following it will be ignored.
ParseFloat () is parsed only in decimal format, so it does not use the second parameter to specify the base number.
If the string contains a number that can be parsed as a positive number (no decimal point or zero decimal point), parseFloat () returns an integer.
Example:
var num1 = parseFloat("123AF"); //123 var num2 = parseFloat("0xA"); //0 var num3 = parseFloat("22.5"); //22.5 var num4 = parseFloat("22.3.56"); //22.3 var num5 = parseFloat("0908.5"); //908.5
The difference between parseInt () and parseFloat () is:
- The first decimal point in the string parsed by parseFloat () is valid, and parseInt () stops parsing when it encounters a decimal point, because the decimal point is not a valid numeric character.
- ParseFloat () always ignores the leading zero, and the string in hexadecimal format is always converted to 0, while the second parameter of parseInt () can set the base, convert according to the base number.
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!