Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Funktion (Größe formatieren) umkehren (https://www.delphipraxis.net/173466-funktion-groesse-formatieren-umkehren.html)

atc 26. Feb 2013 15:19

AW: Funktion (Größe formatieren) umkehren
 
danke für die hilfe. ich habe es jetzt so. meint ihr es geht noch besser?
Delphi-Quellcode:
function FormatSizeR(S: string): Int64;
var
  I: Integer;
  E: Extended;
begin
  I := Pos(' ', S);
  Assert(I > 0);
  E := StrToFloat(Copy(S, 1, Pred(I)));
  S := Copy(S, Succ(I));
  case S[1] of
    'B': I := 0;
    'K': I := 1;
    'M': I := 2;
    'G': I := 3;
    'T': I := 4;
    'P': I := 5;
    'E': I := 6;
    'Z': I := 7;
    'Y': I := 8;
  end;
  Result := Round(E * (1 shl (I * 10)));
end;

Uwe Raabe 26. Feb 2013 15:19

AW: Funktion (Größe formatieren) umkehren
 
Mit Hilfe der Unit StrUtils kann man die Funktion nahezu 1:1 umkehren:

Delphi-Quellcode:
function FormatSizeR(S: string): Int64;
const
  UNITS: Array[0..8] of string = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB',
                                  'ZB', 'YB');
var
  I: Integer;
  E: Extended;
  iIndex: Integer;
begin
  I := Pos(' ', S);
  Assert(I > 0);
  E := StrToFloat(Copy(S, 1, Pred(I)));
  S := Copy(S, Succ(I));
  iIndex := IndexText(S, UNITS);
  if iIndex < 0 then
    raise Exception.CreateFmt('ungültige Einheit "%s"', [S]);
  Result := Round(E * (1 shl (iIndex * 10)));
end;

atc 26. Feb 2013 15:26

AW: Funktion (Größe formatieren) umkehren
 
Danke, das ist auch gut. Komplett ohne Hilfsfunktion, if oder case geht es wohl nicht.
Sowas geht ja leider nicht, oder?
Delphi-Quellcode:
const
  A: Array[Char] of Byte = ('B': 0, 'K': 1, 'M': 2, usw.);


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:21 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz