DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

ExchangeInt

Exchanges the values of two given integers.

procedure ExchangeInt(var I1, I2: Integer);
var
  Temp: Integer;  // temporary value used when exchanging values
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Byte overload)

Exchanges the values of two given unsigned 8 bit integers.

procedure Exchange(var I1, I2: Byte); overload;
var
  Temp: Byte;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Int64 overload)

Exchanges the values of two given signed 64 bit integers.

procedure Exchange(var I1, I2: Int64); overload;
var
  Temp: Int64;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Longint overload)

Exchanges the values of two given signed 32 bit integers.

procedure Exchange(var I1, I2: Longint); overload;
var
  Temp: Longint;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Longword overload)

Exchanges the values of two given unsigned 32 bit integers.

procedure Exchange(var I1, I2: Longword); overload;
var
  Temp: Longword;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Shortint overload)

Exchanges the values of two given signed 8 bit integers.

procedure Exchange(var I1, I2: Shortint); overload;
var
  Temp: Shortint;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Smallint overload)

Exchanges the values of two given signed 16 bit integers.

procedure Exchange(var I1, I2: Smallint); overload;
var
  Temp: Smallint;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (UInt64 overload)

Exchanges the values of two given unsigned 64 bit integers.

procedure Exchange(var I1, I2: UInt64); overload;
var
  Temp: UInt64;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

Exchange (Word overload)

Exchanges the values of two given unsigned 16 bit integers.

procedure Exchange(var I1, I2: Word); overload;
var
  Temp: Word;
begin
  Temp := I1;
  I1 := I2;
  I2 := Temp;
end;

IntToRoman

Converts non negative integer AValue into a string containing its roman numeral representation.

function IntToRoman(AValue: Word): string;
const
  Values: array[1..13] of Word = (
    1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000
  );
  RomanNumerals: array[Low(Values)..High(Values)] of string = (
    'I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'
  );
var
  Idx: Low(Values)..High(Values);
begin
  Assert(AValue > 0);
  Result := '';
  Idx := High(Values);
  while AValue > 0 do
  begin
    while Values[Idx] > AValue do
      Dec(Idx);
    Dec(AValue, Values[Idx]);
    Result := Result + RomanNumerals[Idx];
  end;
end;

IsValidDouble

Checks if the given string represents a valid double precision floating point number.

function IsValidDouble(const S: string): Boolean;
var
  Dummy: Double;
begin
  Result := SysUtils.TryStrToFloat(S, Dummy);
end;

IsValidExtended

Checks if the given string represents a valid extended precision floating point number.

function IsValidExtended(const S: string): Boolean;
var
  Dummy: Extended;
begin
  Result := SysUtils.TryStrToFloat(S, Dummy);
end;

IsValidSingle

Checks if the given string represents a valid single precision floating point number.

function IsValidSingle(const S: string): Boolean;
var
  Dummy: Single;
begin
  Result := SysUtils.TryStrToFloat(S, Dummy);
end;

MSecToDateTime

Converts the given number of milliseconds to a TDateTime value.

function MSecToDateTime(const MSecs: Int64): TDateTime;
begin
  Result := MSecs / SysUtils.MSecsPerDay;
end;

View the whole database.

Go to the DelphiDabbler website.