The difference between Number(), parseInt() and parseFloat() in js

One: Number()

If it is a Boolean value, true and false values ​​will be converted to 1 and 0 respectively.

If it's a numeric value, it's simply passed in and returned.

Returns 0 if it is a null value.

If undefined, return NaN.

If it is a string:

  a. If the string contains only numbers, convert it to a decimal value, ignoring the leading 0

  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 above format, convert it to NaN

If it is an object, call the object's valueOf() method, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, call the object's toString() method, and then convert the returned string value according to the previous rules.

example: 

?
1
2
3
var num1 = Number( "Hello world" );       //NaN
var num2 = Number( "" );             //0
var num3 = Number( "0000011" );        //11

二:parseInt()

parseInt() is more commonly used when dealing with integers. When the parseInt() function converts a string, it ignores the spaces in front of the string until it finds the first non-space character.

If the first character is not a digit or a minus sign, parseInt() will return NaN, and similarly, converting an empty string with parseInt() will return NaN.

If the first character is a numeric character, parseInt() continues parsing the second character until all subsequent strings have been parsed or a non-numeric character is encountered.

The parseInt() method also has base mode, which can convert binary, octal, hexadecimal, or any other base string to an integer.

The base is specified by the second parameter of the parseInt() method, so to parse the hexadecimal value, of course, for binary, octal, and even decimal (the default mode), the parseInt() method can be called like this.

example:

?
1
2
3
4
var num1 = parseInt( "AF" ,16);            //175
    var num2 = parseInt( "AF" );             //NaN
    var num3 = parseInt( "10" ,2);            //2  (按照二进制解析)
    var num4 = parseInt( "sdasdad" );          //NaN

Three: parseFloat()

 Similar to the parseInt() function, parseFloat() parses each character starting from the first character (position 0). It also parses until the end of the string, or until an invalid floating-point numeric character is encountered.

 That is to say, the first decimal point in the string is valid, and the second decimal point is invalid, and the string after it will be ignored.

 parseFloat() only parses decimal, so it doesn't have the usage of the radix specified by the second parameter

 If the string contains a parseable positive number (no decimal point, or all zeros after the decimal point), parseFloat() returns an integer.

example:

?
1
2
3
4
5
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, while parseInt() stops parsing when it encounters a decimal point because the decimal point is not a valid numeric character.
  • parseFloat() always ignores leading zeros, strings in hexadecimal format are always converted to 0, and the second parameter of parseInt() can set the base, and convert according to the base of this base.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325207477&siteId=291194637