DelphiBasics
  Home  |  Integer and floating point numbers
 Documents
 Tutorials

 Writing your first program
 Writing your second program
 Amending this program
 
 Delphi data types
   Numbers
   Text
   Sets and enumerations
   Arrays
   Records
 
 Programming logic
   Looping
   SubRoutines
   Exception handling
 
 Dates and times
 
 Files
 
 Pointers
 
 Printing text and graphics
 
 Object Orientation basics
   Memory leaks!
   Inheritance
   Abstraction
   Interfaces
   An example class
 
 References

 Standard components
 
 Articles

 Learn to program in Pascal - online education course

 Castalia for Delphi
 A brief history of Delphi
 
 Usability : file handling
 Usability : reference books
 
  Integer and floating point numbers
The different number types in Delphi
Delphi provides many different data types for storing numbers. Your choice depends on the data you want to handle. In general, smaller number capacities mean smaller variable sizes, and faster calculations. Ideally, you should use a type that comfortably copes with all possible values of the data it will store.
 
For example, a Byte type can comfortably hold the age of a person - no-one to date has lived as long as 255 years.
 
With decimal numbers, the smaller capacity types also have less precision. Less numbers of significant digits. Let us look at the different types:
 
 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.


Assigning to and from number variables
Number variables can be assigned from constants, other numeric variables, and expressions:
 
 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
Number calculations, or expressions, have a number of primitive operators available:
 
 +   : 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

When using these multiple operators in one expression, you should use round brackets to wrap around sub-expressions to ensure that the result is obtained. This is illustrated in the examples below:
 
 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 functions and procedures
Delphi provides many builtin functions and procedures that can perform numeric calculations. Some examples are given below - click on any to discover more. Note that these routines are stored in Units that are shipped with Delphi, and which form part of the standard delphi Run Time Library. You will need to include a reference to the Unit in order to use it (the code example provided with each gives the unit name and shows how to refer to it).
 
 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


Converting from numbers to strings
Delphi also provides routines that convert numbers into strings. This is often useful for display purposes.
 
 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


Converting from strings to numbers
Finally, Delphi provides string to number conversion utilities. Here are some examples:
 
 StrToInt     Converts an integer string into an integer
 StrToIntDef  Fault tolerant version of StrToInt
 StrToFloat   Converts a decimal string to a number

 

Delphi Programming Neil Moffatt 2002 - 2013. All rights reserved.  |  Contact the author  |  Home Page