Registriert seit: 1. Jul 2007
155 Beiträge
|
Re: Dateigröße aufteilen in kB, MB,...
30. Jul 2007, 17:56
Und kommt dem hier schon recht nah^^:
Lösung1 von mir:
Delphi-Quellcode:
function FileSizeToString(const FileSize: Int64): String;
const Measure: Array[0..4] of String = ('Byte', 'KB', 'MB', 'GB', 'TB');
var NewSize: Currency;
var i: Integer;
begin
Result := '';
if FileSize > -1 then
begin
i := 0;
NewSize := FileSize;
while (NewSize >= 1024) do
begin
NewSize := NewSize / 1024;
Inc(i);
end;
case i of
0: Result := FloatToStr( NewSize );
1,
2,
3: Result := FormatFloat('0.00', NewSize);
4: Result := FormatFloat('0.000', NewSize);
else Result := FloatToStr( NewSize );
end;
Result := Result + #32 + Measure[i];
end;
end;
Lösung2 von himitsu:
Delphi-Quellcode:
Function SizeToString(Size: LargeInt): String;
Var i: LargeInt;
Begin
i := Abs(Size);
If i < 1000 Then Result := Format('%.0n B', [Size / 1])
Else If i < 10235 Then Result := Format('%.2n KB', [Size / 1024])
Else If i < 102349 Then Result := Format('%.1n KB', [Size / 1024])
Else If i < 1023488 Then Result := Format('%.0n KB', [Size / 1024])
Else If i < 10480518 Then Result := Format('%.2n MB', [Size / 1048576])
Else If i < 104805172 Then Result := Format('%.1n MB', [Size / 1048576])
Else If i < 1048051712 Then Result := Format('%.0n MB', [Size / 1048576])
Else If i < 10732049531 Then Result := Format('%.2n GB', [Size / 1073741824])
Else If i < 107320495309 Then Result := Format('%.1n GB', [Size / 1073741824])
Else If i < 1073204953088 Then Result := Format('%.0n GB', [Size / 1073741824])
Else If i < 10989618719622 Then Result := Format('%.2n TB', [Size / 1099511627776])
Else If i < 109896187196212 Then Result := Format('%.1n TB', [Size / 1099511627776])
Else If i < 1098961871962112 Then Result := Format('%.0n TB', [Size / 1099511627776])
Else If i < 11253369568892027 Then Result := Format('%.2n PB', [Size / 1125899906842624])
Else If i < 112533695688920269 Then Result := Format('%.1n PB', [Size / 1125899906842624])
Else If i < 1125336956889202688 Then Result := Format('%.0n PB', [Size / 1125899906842624])
Else Result := Format('%.2n EB', [Size / 1152921504606846976]);
End;
|
|
Zitat
|