The difference between number (), parseint (), and parsefloat () in JS

Source: Internet
Author: User

First: Number()

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

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

If it is a null value, 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, 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 valueOf() method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, call the toString() method of the object, and then convert the returned string value according to the previous rules.

example: 

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


Second: parseInt()

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

If the first character is not a number or a negative sign, parseInt() will return NaN. Similarly, using parseInt() to convert an empty string will also return NaN.

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 a base mode, which can convert a string of binary, octal, hexadecimal or any other base into 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   (According to binary analysis)
   Var num4 = parseInt("sdasdad");         //NaN
Third: parseFloat()

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

 In other words, 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 does not have the second parameter to specify the usage of the radix

 If the string contains a number that can be parsed as a positive number (no decimal point, or zero after the decimal point), parseFloat() will return 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, while parseInt() will stop parsing when it encounters a decimal point, because the decimal point is not a valid number character.
parseFloat() will always ignore the leading zeros, and the hexadecimal format string will always be converted to 0, and the second parameter of parseInt() can set the radix and convert it according to the hexadecimal of this radix.
The difference between Number(), parseInt() and parseFloat() in js

Label: item usage octal invalid is syn command nbsp sixteen

Original address: https://www.cnblogs.com/luckyXcc/p/8985241.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.