|
Written by JAK Olivier
|
|
Jun 27, 2009 at 09:16 AM |
|
Delphi distinguishes in the way it uses text
(string, char) and numbers (integer, real, etc.).
Variables are declared as integers if
they use whole numbers (for example 23 or 765) and real if
they have decimal fractions (for example 2.63 or 0.46544). Apart from the data type Integer other related types include
(ranges included):
Shortint –128..127 Smallint –32768..32767 Longint –2147483648..2147483647 Int64 –2^63..2^63–1 Byte 0..255 Word 0..65535 Longword 0..4294967295
Apart from the data type Real other
related types include (ranges included):
Real48 2.9 x 10^–39 .. 1.7 x 10^38 Single 1.5 x 10^–45 .. 3.4 x 10^38 Double 5.0 x 10^–324 .. 1.7 x 10^308 Extended 3.6 x 10^–4951 .. 1.1 x 10^4932 Comp –2^63+1 .. 2^63 –1 Currency –922337203685477.5808.. 922337203685477.5807
Mathematical operators can be used in
Delphi:
| Operation |
Delphi Operator |
Datatype(s) |
Example |
| Addition + |
+ |
Integer /
Real |
iX := iA
+ iB; |
| Subtraction - |
- |
Integer /
Real |
rX := rA
- rB; |
| Multplication × |
* |
Integer /
Real |
iX := iA
* iB; |
| Division ÷ |
/ |
Integer /
Real (answer Real) |
rH := iD
/ iS; |
| Integer division |
DIV |
Integer |
iX := iA
DIV iB; |
| Remainder after division |
MOD |
Integer |
iX := iA
MOD iB; |
Mathematical functions and procedures also exist that makes conversion
between text and numbers and the manipulation of numbers easier:
|
Function / Procedure
|
Purpose
|
Example
|
|
StrToInt
|
Converts string value to integer
|
iN :=
StrToInt(sNumber);
|
|
IntToStr
|
Converts integer value to string
|
sNumber :=
IntToStr(iN);
|
|
StrToFloat
|
Converts string value to real
|
rN :=
StrToFloat(sNumber);
|
|
FloatToStr
|
Converts real value to string
|
sNumber :=
FloatToStr(rN);
|
|
FloatToStrF
|
Converts real value to string with formatting (1 number after the
decimal)
|
sN :=
FloatToStrF(rN, ffFixed, 5, 1);
|
|
Val
|
Converts string value to integer or real. Also checks for errors.
|
Val(sOriginal,
iNum, iError);
Val(sOriginal,
rNum, iError);
|
|
Trunc
|
Truncates (cuts) the decimal point
|
Trunc(4.5);
|
|
Round
|
Rounds real number to the nearest integer
|
Round(4.8);
|
|
Roundto
|
Rounds to a set power of 10
|
iH :=
Roundto(2745,3);
|
|
Inc
|
Increases value of variable
(You can also do this as: iNumber := iNumber + 1;)
|
Inc(iNumber)
|
|
Dec
|
Decreases value of variable (You can also do this as: iNumber := iNumber - 1;)
|
Dec(iNumber)
|
|
Frac
|
Provides decimal part of real number
|
rK :=
Frac(3.54);
|
|
Sqr
|
Gives square of number typed in
|
rK := Sqr(16);
|
|
Sqrt
|
Gives square root of a number
|
rK := Sqrt(16);
|
|
Power
|
Raises first number to the power of the second number (134)
|
Power(13,4)
|
|
Pi
|
Provides the value of pi (π)
|
rW := rK * Pi;
|
|
Random
|
Provides a random number within a range of 0 and limit-1.
|
Randomize; iX := Random(100); (iX = any
number between 0 and 99)
|
|
|
Last Updated ( Jun 27, 2009 at 08:16 AM )
|