Differences between JavaScript parseint and number Functions

Source: Internet
Author: User

However, parseint ("08", 10) can return 8.

To clarify the differences between the two,

See the difference between parseint and number written by others:

Parseint
Parses a string argument and returns an integer of the specified Radix or base.
Core functions
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)
Parameters
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 ")
-----------------------------------------------
-----------------------------------------------
Converts a specified object to a number.
Core functions
Implement Navigator 4.0 and Netscape Server 3.0

Syntax
Number (OBJ)
Parameters
OBJ is an object.

Description
If the object is a date object, number will return the number of milliseconds that have elapsed since January 1, January 1, 1970, the Greenwich Mean Time. After this date, it is a positive number, and the previous value is a negative number.
If obj is a string without a number, number returns Nan.

Example
The following example converts a date object to a numeric value:
<SCRIPT>
D = new date ("December 17,199 5 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.