The parseint and number functions in JavaScript

Source: Internet
Author: User

Function Effect:
Parseint Converts a string (string) type to an integer type.


The number () function converts the value of the object to numbers.




Different syntax:

parseint (string, [radix])


String: The required number of parameters. The string to be converted

Radix: Optional, the cardinality of the number. The value range is 2~36.

Assume that the number of references is less than 2 or greater than 36. Then parseint () will return NaN.


<pre name= "Code" class= "JavaScript" >        alert ("ParseInt1 return" + parseint ("1", 1));   Returns nan        alert ("parseint0x123" + parseint ("0x123"));  291        alert ("parseInt000123" + parseint ("000123"));  123


When the value of the radix is 0, or if the parameter is not set, parseint () infers the cardinality of the number based on string.

Assuming that string starts with "0x", parseint () resolves the remainder of the string to a 16-binary integer.

Assuming that string starts with 0, then ECMAScript V3 agrees that an implementation of parseint () parses the subsequent character into octal or hexadecimal digits.

Suppose that string starts with a number from 1 to 9, and parseint () parses it into a decimal integer.

Number (object)

number returns a digit whose number is the object.


Assume that the value of an object cannot be converted to a number. Then the number () function returns NaN.


        var test = new Date ()        var teststr = new String ("123");        var testStr1 = new String ("5,000");        Alert ("Number (test)" + number (test));  Returns 1437631091369        alert ("Number (TESTSTR)" + number (TESTSTR)); Returns 123        alert ("Number (TESTSTR1)" + number (TESTSTR1)); Return Nan

  in  var testStr1 = new String ("5,000"), the string contains a thousand character. This causes the conversion to be impossible. Although it is very small in detail, it should be cautious.

Contains thousands of characters of the string, can not be directly converted, the first to remove the thousand character, and then the conversion, this is the result of this blog.

parseint and number functions in JavaScript

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.