The parseInt AND Number FUNCTIONS In JavaScript, javascriptparseint

Source: Internet
Author: User

The parseInt AND Number FUNCTIONS In JavaScript, javascriptparseint
Function functions:
ParseInt converts the String type to the integer type.
The Number () function converts the value of an Object to a Number.


Syntax differences:

parseInt(string, [radix])


String: A required parameter. The string to be converted.

Radix: (optional) base number of a number. The value range is 2 ~ 36.

If this parameter is smaller than 2 or greater than 36, parseInt () returns NaN.


<Pre name = "code" class = "javascript"> alert ("parseInt1 returns" + parseInt ("1", 1 )); // return NaN alert ("parseInt0x123" + parseInt ("0x123"); // 291 alert ("parseInt000123" + parseInt ("000123"); // 123


 

When the value of the radix parameter is 0, or this parameter is not set, parseInt () determines the base number based on the string. If the string starts with "0x", parseInt () resolves the rest of the string to a hexadecimal integer. If the string starts with 0, ECMAScript v3 allows an implementation of parseInt () to parse the subsequent characters into octal or hexadecimal numbers. If string is 1 ~ Start with a number of 9, parseInt () will resolve it to a decimal integer.

Number(object)

Number returns a Number and its parameter is an object.


If the object value cannot be converted to a Number, 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 kilobytes of characters, and therefore cannot be converted. Although it is a small detail, you should be cautious. A string containing a kilobytes of characters cannot be directly converted. You need to remove the kilobytes of characters before conversion, which leads to this blog.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.