Thema: Delphi Bytes in GB formatieren?

Einzelnen Beitrag anzeigen

Hawkeye219

Registriert seit: 18. Feb 2006
Ort: Stolberg
2.227 Beiträge
 
Delphi 2010 Professional
 

Re: Bytes in GB formatieren?

  Alt 22. Feb 2007, 13:12
Hallo,

falls die Laufwerke mal etwas kleiner oder größer sind:

Delphi-Quellcode:
function FormatFileSize (aSize: Int64): string;

const
  MB = Int64(1024 * 1024);
  GB = Int64(1024 * MB);
  TB = Int64(1024 * GB);
  PB = Int64(1024 * TB);

  cSize : array [0..5] of Int64 = (1, 1024, MB, GB, TB, PB);
  cLabel : array [0..5] of string = ('B', 'KB', 'MB', 'GB', 'TB', 'PB');

var
  lIndex : Integer;

begin
  lIndex := Ord(aSize >= cSize[1]) + Ord(aSize >= cSize[2])
            + Ord(aSize >= cSize[3]) + Ord(aSize >= cSize[4])
            + Ord(aSize >= cSize[5]);
  Result := Format('%g %s', [Int(100.0 * aSize / cSize[lIndex]) / 100, cLabel[lIndex]]);
end;
Gruß Hawkeye
  Mit Zitat antworten Zitat