Flash / Flex / ActionScript/Data Type/parseInt

Материал из Web эксперт
Перейти к: навигация, поиск

If omitted, the radix is assumed to be 10, unless the string starts with 0x, 0X, or 0

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       trace(parseInt("0x12"));     // The radix is implicitly 16. Displays: 18
       trace(parseInt("017"));      // The radix is implicitly 8. Displays: 15
   }
 }

}

       </source>
   
  


Octal is treated as a decimal number, not an octal number:

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       // The number is treated as a decimal, not an octal number
       trace(parseInt("017",  10));   // Displays: 17 (not 15)
   }
 }

}

       </source>
   
  


Parse the numbers from the string base-10

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       trace(parseInt("17", 10));     // Displays: 17
   }
 }

}

       </source>
   
  


Parse the numbers from the string base-16 (hexadecimal)

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       trace(parseInt("19", 16));     // Displays: 25
   }
 }

}

       </source>
   
  


Parse the numbers from the string in base-2 (binary)

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       trace(parseInt("110011", 2));  // Displays: 51
   }
 }

}

       </source>
   
  


When the number is treated base-10, conversion stops when a non-numeric character is encountered

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       // The number is treated as a decimal, not a hexadecimal number
       trace(parseInt("0x12", 10));   // Displays: 0 (not 12 or 18)
   }
 }

}

       </source>