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.