The difference between JavaScript parseint and number function _javascript skills

Source: Internet
Author: User
Tags arithmetic numeric value
But parseint ("08", 10) can return 8.

To find out the difference between the two,

Refer to the difference written by others: Parseint&number

parseint
Parses a string argument and returns an integer of the specified radix or base.
Core functions
Implementation version Navigator 2.0:if the ' the ' of the ' the ' of the ' string specified in parseint (string) cannot is converted to a number, re Turns "NaN" on Solaris and Irix and 0 in all other platforms. Navigator 3.0, Livewire 2.0:returns "NaN" on all platforms if the only character of the string specified in parseint (str ing) cannot is converted to a number.



Grammar
parseint (String,radix)
Parameters
String A string that represents the value of your want to parse.
Radix (Optional) An integer that represents the radix of the return value.



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

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

If The radix is isn't specified or is specified as 0, JavaScript assumes the following:



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

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

If the input string begins with or, the Radix is (decimal).
If the character cannot is converted to a number, parseint returns "NaN".
For arithmetic purposes, the ' NaN ' value is ' not ' a number in any radix. You can called the isNaN function to determine if the result of parseint is "NaN". If "Nan" is passed on to arithmetic operations, the operation results would 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", ten) The following example all return "NaN":

parseint ("Hello", 8)
parseint ("0x7", 10)
parseint ("FFF") even though the radix is specified differently, the following example all return because the input string Begins with "0x".

parseint ("0x11", 16)
parseint ("0x11", 0)
parseint ("0x11")
-----------------------------------------------
-----------------------------------------------
Converts the specified object to a number.
Core functions
Implementation version Navigator 4.0, Netscape Server 3.0

Grammar
Number (obj)
Parameters
Obj an object.



Describe
If the object is a date-type object, number returns the milliseconds that have elapsed since the Greenwich Mean time of January 1, 1970, after which the positive numbers are negative.
If obj is a string that has no numeric format, number returns NaN.


Example
The following example converts a Date object to a numeric value:
<SCRIPT>
D = new Date ("December 17, 1995 03:24:00");
document.write (number (d) + "<BR>");
Related Article

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.