DelphiDabbler Code Snippets Database

Snippet Selection

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

Percentage

Calculates and returns the value of A / B as a percentage, rounded to the nearest whole number. B must be non-zero.

function Percentage(A, B: Integer): Integer;
begin
  Result := Round((A / B) * 100);
end;

PercentageStr

Calculates and returns the value of A / B as a percentage string, rounded to the nearest whole number. B must be non-zero.

function PercentageStr(A, B: Integer): string;
begin
  Result := SysUtils.IntToStr(Percentage(A, B)) + '%';
end;

TempFileNameEx

Returns a unique temporary file name in the directory specified in APath. The file name includes the first three characters of AStub followed by hexadecimal characters. If ACreate is True then the file is created. Returns an empty string on failure.

function TempFileNameEx(const APath: string; const AStub: string;
  const ACreate: Boolean): string;
begin
  SetLength(Result, Windows.MAX_PATH);
  if Windows.GetTempFileName(
    PChar(APath), PChar(AStub), 0, PChar(Result)
  ) <> 0 then
  begin
    if not ACreate then
      // user doesn't want file creating so delete it
      SysUtils.DeleteFile(Result);
  end
  else
    Result := '';
end;

View the whole database.

Go to the DelphiDabbler website.