Wednesday, October 25, 2017

ປະເພດຕົວເລກຕ່າງໆໃນ Delphi (Lesson 3)

ໃນ delphi ມີຫລາຍປະເພດ Number ຂື້ນຢູ່ກັບທ່ານເລືອກໃຊ້ໃນຊອບແວຂອງທ່ານ ດັ່ງລຸ່ມນີ້

 Type  Storage size                        Range            
 
 Byte       1                             0 to 255
 ShortInt   1                          -127 to 127
 Word       2                             0 to 65,535
 SmallInt   2                       -32,768 to 32,767
 LongWord   4                             0 to 4,294,967,295
 Cardinal   4*                            0 to 4,294,967,295
 LongInt    4                -2,147,483,648 to 2,147,483,647
 Integer    4*               -2,147,483,648 to 2,147,483,647
 Int64      8    -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
 
 Single     4     7  significant digits, exponent   -38 to +38
 Currency   8    50+ significant digits, fixed 4 decimal places
 Double     8    15  significant digits, exponent  -308 to +308
 Extended  10    19  significant digits, exponent -4932 to +4932
 
 * Note : the Integer and Cardinal types are both 4 bytes in size at present (Delphi release 7), but are not guaranteed to be this size in the future. All other type sizes are guaranteed.

ການກຳໜົດໃຫ້ ຕົວປ່ຽນ Number

const
   YOUNG_AGE = 23;         // Small integer constant
   MANY      = 300;        // Bigger integer constant
   RICH      = 100000.00;  // Decimal number : note no thousand commas
 
 var
   Age       : Byte;       // Smallest positive integer type
   Books     : SmallInt;   // Bigger signed integer
   Salary    : Currency;   // Decimal used to hold financial amounts
   Expenses  : Currency;
   TakeHome  : Currency;
 
 begin
   Age       := YOUNG_AGE; // Assign from a predefined constant
   Books     := MANY + 45; // Assign from a mix of constants (expression)
   Salary    := RICH;      // Assign from a predefined constant
   Expenses  := 12345.67;  // Assign from a literal constant
   TakeHome  := Salary;    // Assign from another variable
   TakeHome  := TakeHome - Expenses;  // Assign from an expression
 end;

ຜົນໄດ້ແມ່ນ

 Age       is set to 23
 Books     is set to 345
 Salary    is set to 100000.00
 Expenses  is set to 12345.67
 TakeHome  is set to 87654.33

Numerical operators
ສຳລັບການປະມວນ ໃຊ້ສັນຍາລັກຄື

 +   : Add one number to another
 -   : Subtract one number from another
 *   : Multiply two numbers
 /   : Divide one decimal number by another
 div : Divide one integer number by another
 mod : Remainder from dividing one integer by another


ຕົວຢ່າງ

var
   myInt : Integer;  // Define integer and decimal variables
   myDec : Single;
 
 begin
   myInt := 20;           // myInt is now 20
   myInt := myInt + 10;   // myInt is now 30
   myInt := myInt - 5;    // myInt is now 25
   myInt := myInt * 4;    // myInt is now 100
   myInt := 14 div 3;     // myInt is now 4   (14 / 3 = 4 remainder 2)
   myInt := 14 mod 3;     // myInt is now 2   (14 / 3 = 4 remainder 2)
 
   myInt := 12 * 3 - 4;   // myInt is now 32  (* comes before -)
   myInt := 12 * (3 - 4); // myInt is now -12 (brackets come before *)
 
   myDec := 2.222 / 2.0;  // myDec is now 1.111
 end;

ຟັງຊັນ Numeric ແລະ procedures
ມີດັ່ງນີ້

 Abs  Returns the absolute value of a signed number
 Max  Gives the maximum of two integer values
 Min  Gives the minimum of two integer values
 Mean Gives the average of a set of numbers
 Sqr  Gives the square of a number
 Sqrt Gives the square root of a number
 Exp  Gives the exponent of a number
 Shl  Shifts the bits in a number left
 Shr  Shifts the bits in a number right
 Tan  Gives the Tangent of a number
 Cos  Gives the Cosine of a number
 Sin  Gives the Sine of a number

ການປ່ຽນ ຈາກ numbers ເປັນ strings

 Str       Converts a number to a string in a simple manner
 CurrToStr Converts a Currency variable to a string
 Format    Number to string conversion with formatting
 IntToStr  Converts an integer to a string
 IntToHex  Converts a number into a hexadecimal string

ການປ່ຽນຈາກ strings ເປັນ numbers

 StrToInt     Converts an integer string into an integer
 StrToIntDef  Fault tolerant version of StrToInt
 StrToFloat   Converts a decimal string to a number

[tag]Delphi, Number, type[/tag]

Subscribe

  • RSS Atom

ອອນລາຍ: 1 | ມື້ນີ້: 13 | ວານນີ້: 25 | ທິດນີ້: 93 | ເດືອນນີ້: 872 | ປີນີ້: 11832 | ລວມ: 78935