hallo,
ich versuche gerade eine umkehrfunktion zu dieser funktion zu schreiben:
Delphi-Quellcode:
function FormatSize(ASize: Int64): string;
const
UNITS: Array[0..8] of string = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB',
'ZB', 'YB');
var
iIndex: Integer;
begin
if ASize <= 0 then
Result := '0,00 B'
else
begin
iIndex := Trunc(ln(ASize) / ln(2) / 10);
Result := Format('%f %s', [ASize / (1 shl (iIndex * 10)), UNITS[iIndex]]);
end;
end;
FormatSize(5662353442) ergibt z.B. 5,27 GB
jetzt brauche ich eine funktion FormatSizeR, die beim Aufruf
FormatSizeR('5,27 GB') exakt den wert 5662353442 zurückliefert.
mein kläglicher ansatz sieht bisher so aus:
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));
if S = 'KB' then
E := E * 1024
else if S = 'MB' then
E := E * 1024 * 1024
else if S = 'GB' then
E := E * 1024 * 1024 * 1024
else if S = 'TB' then
E := E * 1024 * 1024 * 1024 * 1024;
Result := Trunc(E);
end;
allerdings ist das ziemlich umständlich und der wert stimmt auch nicht. kann man das mit ein wenig mathe nicht auch so elegant machen wie die andere funktion?