Explain the difference between JS number (), parseint () and parsefloat () _javascript Skills

Source: Internet
Author: User
Tags numeric numeric value

One: Number ()

If this is a Boolean value, True and false values are converted to 1 and 0, respectively.

If it is a numeric value, just simple incoming and return.

If it is a null value, returns 0.

If it is 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", converts it to the corresponding floating-point number, ignoring the leading 0

C. If the string contains a valid hexadecimal format, such as "0xf", converts 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, the valueof () method of the object is called, and the value returned by the previous rule transformation is converted. If the result of the conversion is Nan, the object's ToString () method is called, and then the string value returned in accordance with the preceding rule transformation.

Cases:

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

Two: parseint ()

parseint () is more commonly used when dealing with integers. When converting a string, the parseint () function ignores the space preceding the string, knowing that the first non-whitespace character is found.

If the first character is not a number or a minus sign, parseint () returns Nan, and similarly, converting the empty string with parseint () returns Nan.

If the first character is a numeric character, parseint () continues 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 pattern that converts binary, octal, hexadecimal, or any other string of strings into integers.

The base is specified by the second parameter of the parseint () method, so the parseint () method can be called to parse hexadecimal values, of course, for binary, octal, or even decimal (default mode).

Cases:

 var num1 = parseint ("AF", 16);            175
var num2 = parseint ("AF");           NaN
var num3 = parseint ("10", 2);         2 (according to binary parsing)
var num4 = parseint ("Sdasdad"); NaN

Three: Parsefloat ()

Like the parseint () function, parsefloat () also parses each character starting at the first character (position 0). is parsed until the end of the string, or until an invalid floating-point number character is encountered.

That is, the first decimal point in the string is valid, and the second decimal point is invalid, and the string behind it is ignored.

parsefloat () resolves only the decimal, so it does not have a second parameter to specify the usage of the cardinality

If the string contains a number that can be resolved to positive numbers (no decimal point, or 0 after the decimal point), parsefloat () returns an integer.

Cases:

 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 () encounters a decimal point that stops parsing because the decimal point is not a valid numeric character.
    • Parsefloat () always ignores leading 0, hexadecimal-formatted strings are always converted to 0, and parseint () the second parameter can set the cardinality, which is converted according to the cardinality.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.