![]() |
Dateigröße aufteilen in kB, MB,...
Hallo,
ich steh gerade auf dem Schlauch. Ich hab eine Dateigröße wie z. b. eine LongInt-Variable die den Wert 4722. Das ist eine Dateilänge. Wie kann ich am Einfachsten diesen Wert aufteilen in kB, MB, ...? |
Re: Dateigröße aufteilen in kB, MB,...
Die Länge der Datei wird in Byte angegeben...also musst Du teilen...je nachdem ob Du kb oder mb willst...entsprechend durch 1024 oder nochmal durch 1024.
Sherlock |
Re: Dateigröße aufteilen in kB, MB,...
gibts dafür ne fertige Funktion?
|
Re: Dateigröße aufteilen in kB, MB,...
Ja :wink: ,
Delphi-Quellcode:
Hier musste aber erst noch kopieren :lol:
function ByteToMegaByte(Byte: Integer): Integer;
begin result := Byte div 1024 div 1024; end; |
Re: Dateigröße aufteilen in kB, MB,...
Ein Quick&Dirty hack in C#:
Code:
Ist aber recht flott zu übersetzen :)
public string SizeString
{ get { float size = FileSize; int divs = 0; while (size > 1024) { size /= 1024; divs++; } string unit; switch (divs) { case 0: unit = " Byte"; break; case 1: unit = " KB"; break; case 2: unit = " MB"; break; case 3: unit = " GB"; break; default: unit = ""; break; } return (string.Format("{0:F2}",size) + unit); } } |
Re: Dateigröße aufteilen in kB, MB,...
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; |
Re: Dateigröße aufteilen in kB, MB,...
Danke für die Hilfe
Biggs 1. Lösung hat mit gut gefallen |
Re: Dateigröße aufteilen in kB, MB,...
Hi,
Und für noch größere Dateien: Zitat:
Delphi-Quellcode:
Dann kann das Else raus und es gibt keine Probleme in der Zeile:
while (NewSize >= 1024) and (i < 4) do
Delphi-Quellcode:
Mfg
Result := Result + #32 + Measure[i];
FAlter |
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:34 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz