Flash / Flex / ActionScript/Data Type/int

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

An example of a for statement that outputs the numbers from 0 to 999

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       for (var i:int = 0; i < 1000; i++) {
         trace(i);
       }
       trace ("That"s the end.");
   }
 }

}

       </source>
   
  


Combining operators with assignment will use the left-hand side of the expression as the first operand

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var a=10;
       a = a + 10;
       //can be more concisely written as
       a += 10;
   }
 }

}

       </source>
   
  


Contains reusable methods for operations pertaining to int values

   <source lang="java">

package com.adobe.utils {

 import flash.utils.Endian;
 
 /**
  * Contains reusable methods for operations pertaining 
  * to int values.
  */
 public class IntUtil {
   
   /**
    * Rotates x left n bits
    *
    * @langversion ActionScript 3.0
    * @playerversion Flash 9.0
    * @tiptext
    */
   public static function rol ( x:int, n:int ):int {
     return ( x << n ) | ( x >>> ( 32 - n ) );
   }
   
   /**
    * Rotates x right n bits
    *
    * @langversion ActionScript 3.0
    * @playerversion Flash 9.0
    * @tiptext
    */
   public static function ror ( x:int, n:int ):uint {
     var nn:int = 32 - n;
     return ( x << nn ) | ( x >>> ( 32 - nn ) );
   }
   
   /** String for quick lookup of a hex character based on index */
   private static var hexChars:String = "0123456789abcdef";
   
   /**
    * Outputs the hex value of a int, allowing the developer to specify
    * the endinaness in the process.  Hex output is lowercase.
    *
    * @param n The int value to output as hex
    * @param bigEndian Flag to output the int as big or little endian
    * @return A string of length 8 corresponding to the 
    *    hex representation of n ( minus the leading "0x" )
    * @langversion ActionScript 3.0
    * @playerversion Flash 9.0
    * @tiptext
    */
   public static function toHex( n:int, bigEndian:Boolean = false ):String {
     var s:String = "";
     
     if ( bigEndian ) {
       for ( var i:int = 0; i < 4; i++ ) {
         s += hexChars.charAt( ( n >> ( ( 3 - i ) * 8 + 4 ) ) & 0xF ) 
           + hexChars.charAt( ( n >> ( ( 3 - i ) * 8 ) ) & 0xF );
       }
     } else {
       for ( var x:int = 0; x < 4; x++ ) {
         s += hexChars.charAt( ( n >> ( x * 8 + 4 ) ) & 0xF )
           + hexChars.charAt( ( n >> ( x * 8 ) ) & 0xF );
       }
     }
     
     return s;
   }
 }
   

}

       </source>
   
  


Creates a variable named width, of type int, and assigns it the value 25

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var width:int = 25;
       trace(width);
   }
 }

}

       </source>
   
  


Display a sequence of square roots

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       for (var i:Number = 50; i > 2; i = Math.sqrt(i)) {
         trace(i);
       }
   }
 }

}

       </source>
   
  


Include the number variable in a string expression. This will implicitly convert the number to a String.

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       for (var i:int = 99; i > 0; i--) {
           trace(i + "bottles of beer on the wall");
       }
   }
 }

}

       </source>
   
  


parseInt(): interpret the text as a number in an arbitrary base.

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
    trace(parseInt("3.14")); // 3

   }
 }

}

       </source>
   
  


The int type is perfect for counters, and is frequently found in for loops:

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
        for (var i:int = 0; i < 1000; i++){
        }
   }
 }

}

       </source>
   
  


toExponential()

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
         var x = 2 / 3;
         trace(x.toExponential()); 
   }
 }

}

       </source>
   
  


toFixed()

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
         var x = 2 / 3;
         trace(x.toFixed()); 
   }
 }

}

       </source>
   
  


toPrecision()

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
     var x = 2 / 3;
     trace(x.toPrecision("3")); 
   }
 }

}

       </source>
   
  


Use int() to convert string to integer

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
        trace(int("3.14")); // 3

   }
 }

}

       </source>
   
  


You can also use exponential notation to declare numbers.

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
        2.007e3; // 2007
        1414e-3; // 1.414
        1.9e+17; // 190,000,000,000,000,000
   }
 }

}

       </source>