Chuyển đến nội dung chính

parseInt() in javascript

     The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

Syntax

   parseInt(string, radix);

string
     The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.

radix
     An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

Description
     The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices 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. Leading and trailing spaces are allowed.


  1. If radix is undefined or 0 (or absent), JavaScript assumes the following:
  2. If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  3. If the input string begins with "0", radix is eight (octal) or 10 (decimal).  Exactly which radix is chosen is implementation-dependent.  ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.  For this reason always specify a radix when using parseInt.
  4. If the input string begins with any other value, the radix is 10 (decimal).
  5. 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.

To convert number to its string literal in a particular radix use intValue.toString(radix).

The following example return 15.
Example

   parseInt(" 0xF", 16);
   parseInt(" F", 16);
   parseInt("17", 8);
   parseInt(021, 8);
   parseInt("015", 10);
   parseInt(15.99, 10);
   parseInt("15,123", 10);
   parseInt("FXX123", 16);
   parseInt("1111", 2);
   parseInt("15*3", 10);
   parseInt("15e2", 10);
   parseInt("15px", 10);
   parseInt("12", 13);

The following example return NaN.
Example

   parseInt("Hello", 8); // Not a number at all
   parseInt("546", 2);   // Digits are not valid for binary representations

The following example return -15.
Example

   parseInt("-F", 16);
   parseInt("-0F", 16);
   parseInt("-0XF", 16);
   parseInt(-15.1, 10);
   parseInt(" -17", 8);
   parseInt(" -15", 10);
   parseInt("-1111", 2);
   parseInt("-15e1", 10);
   parseInt("-12", 13);

The following example return 224.
Example

   parseInt("0e0", 16);


Example Program:- (Editor)


Editor is Loading...

Advertisement

Nhận xét

Popular Posts

Autodesk ArtCAM 2017 Crack + Patch + Full Version

Autodesk ArtCAM 2017 Crack + Patch + Full Version ArtCAM is, in fact, a design tool designed more for designers than engineers, and allows designers to showcase their creativity. The software delivers a CNC modeling CAD that can be used for a variety of industries. For example, designers of signs, woodcutters, jewelry designers and many others who can produce high-quality products before they can deliver their products in two-dimensional and three-dimensional form. It's easier with this software than ever. Features and Features of ArtCAM 2017 Crack + Patch + Full Version: Support for 64-bit systems Ability to mount jewelry pieces The ability to create complex shapes Ability to cut different pieces of pages The ability to create floating vectors Save layers and shapes that you must use Availability of ready-made models available System Requirements for ArtCAM 2017 full Crack for 2017, 2018: OS: Windows 7 SP1 / 8.x / 10 for 2012: Processor Type & Speed: Intel Core 2 Duo (or equiv...

Move up and down listbox item using javascript

     In the following example,we can easy to move the list box item up and down using javascript code. Example     function listboxMove(listID, direction) {       var listbox = document.getElementById(listID);       var selIndex = listbox.selectedIndex;       if (-1 == selIndex) {         alert("Please select an option to move.");         return;       }       var increment = -1;       if (direction == 'up')         increment = -1;       else         increment = 1;       if ((selIndex + increment) < 0 ||         (selIndex + increment) > (listbox.options.length - 1)) {  ...

Adobe Acrobat Pro DC 2019 Portable (v19.010.20064)

Adobe Acrobat Pro DC 2019 Portable (v19.010.20064) is a software for creating PDF files. With the help of this software, the user can convert all text files, photos and other relevant documents to PDF format. Adobe Acrobat software is popular with many software users due to the compatibility with most software. You can plan it in the printer's configuration area by typing Ctrl + P keys that are specific to print operations, print texts or anything printable in Adobe Acrobat as a PDF. Adobe Acrobat software features Ability to convert to other formats like images and Word Scan directly from the scanner The presence of internal OCR to identify texts Optimized mode for displaying documents as much as possible Specific compatibility with high-quality images to showcase the best of the documents Ability to do things in a batch like conversion Ability to add new actions to PDF documents Ability to interconnect multiple PDF documents Ability to build form System Requirements Operating Sys...