Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.157 Beiträge
 
Delphi 12 Athens
 
#9

Re: Grössenangabe automatische Umwandlung

  Alt 22. Dez 2009, 19:31
Dieser Code ist theoretisch so optimiert, daß es maximal 3 Zahlen im Ergebnis gibt (abgesehn von stsFull)

Delphi-Quellcode:
Type TSTSFormat = (sts2, sts10, sts2Long, sts10Long, stsFull);

Function SizeToString(Const Size: Int64; SFormat: TSizeFormat): String;
  Const D: Array[0..17] of Integer = (1000, 10235, 102349, 1023488, 10480518,
      104805172, 1048051712, 10732049531, 107320495309, 1073204953088,
      10989618719622, 109896187196212, 1098961871962112, 11253369568892027,
      112533695688920269, 1125336956889202688, 0);
    S: Array[sts2..sts10Long] of Array[0..7] of String = (('%.*n B', '%.*n KB',
      '%.*n MB', '%.*n GB', '%.*n TB', '%.*n PB', '%.*n EB'), ('%.*n B', '%.*n KiB',
      '%.*n MiB', '%.*n GiB', '%.*n TiB', '%.*n PiB', '%.*n EiB'), ('%.*n Byte',
      '%.*n Kilobyte', '%.*n Megabyte', '%.*n Gigabyte', '%.*n Terabyte',
      '%.*n Petabyte', '%.*n Exabyte'), ('%.*n Byte', '%.*n Kibibyte', '%.*n Mebibyte',
      '%.*n Gibibyte', '%.*n Tebibyte', '%.*n Pebibyte', '%.*n Exbibyte');
    X: Array[0..7] of Integer = (1, 1024, 1048576, 1073741824, 1099511627776,
      1125899906842624, 1152921504606846976);

  Var i: Integer;

  Begin
    If SFormat < stsFull Then Begin
      i := 0;
      While (i < 17) and (Size < D[i]) do Inc(i);
      Result := Format(S[SFormat, (i + 2) div 3],
        [2 - ((i + 2) mod 3), Size / X[(i + 2) div 3]]);
    Else Result := Format(S[sts2Long, 0], [Size / 1]);
  End;
Hab grad kein Delphi hier, also ist es noch komplett ungetestet.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat