Home  >  Article  >  Web Front-end  >  The difference between javascript parseInt and Number function_javascript skills

The difference between javascript parseInt and Number function_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:36:13861browse

But parseInt("08", 10) can return 8.

To clarify the difference between the two,

referred to the difference between parseInt&Number written by others:

parseInt
Parses a string argument and returns an integer of the specified radix or base.
Core function
Implementation version Navigator 2.0: If the first character of the string specified in parseInt(string) cannot be converted to a number, returns "NaN" on Solaris and Irix and 0 on all other platforms.Navigator 3.0, LiveWire 2.0: Returns "NaN" on all platforms if the first character of the string specified in parseInt(string) cannot be converted to a number.



Syntax
parseInt(string,radix)
Parameter
string A string that represents the value you want to parse.
radix (Optional) An integer that represents the radix of the return value.



Description
The parseInt function is a built-in JavaScript function.
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16) , A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values.

If the radix is ​​not specified or is specified as 0, JavaScript assumes the following:



If the input string begins with "0x", the radix is 16 (hexadecimal).

If the input string begins with "0", the radix is ​​eight (octal).

If the input string begins with any other value, the radix is ​​10 (decimal).
If the first character cannot be converted to a number, parseInt returns "NaN".
For arithmetic purposes, the "NaN" value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is "NaN". If "NaN" is passed on to arithmetic operations, the operation results will also be "NaN".


Example
The following example all return 15:
parseInt("F", 16)
parseInt("17", 8)
parseInt("15", 10)
parseInt(15.99, 10)
parseInt( "FXX123", 16)
parseInt("1111", 2)
parseInt("15*3", 10) The following example all return "NaN":

parseInt("Hello" , 8)
parseInt("0x7", 10)
parseInt("FFF", 10) Even though the radix is ​​specified differently, the following example all return 17 because the input string begins with "0x".

parseInt("0x11", 16)
parseInt("0x11", 0)
parseInt("0x11")
------------- ----------------------------------
---------------- ----------------------------------
Convert the specified object to a number.
Core function
Implementation version Navigator 4.0, Netscape Server 3.0

Syntax
Number(obj)
Parameters
obj an object.



Description
If the object is of type Date, Number returns the number of milliseconds that have elapsed since January 1, 1970 GMT, after this date is a positive number, and the previous one is a negative number.
If obj is a string without number format, Number will return NaN.


Example
The following example will convert a Date object into a numeric value:
<script> <BR>d = new Date ("December 17, 1995 03:24:00 "); <BR>document.write (Number(d) "<BR>");</script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn