AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Formatieren von Dateigrößen

Ein Thema von Sir Rufo · begonnen am 9. Okt 2015 · letzter Beitrag vom 11. Okt 2015
 
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#1

Formatieren von Dateigrößen

  Alt 9. Okt 2015, 18:58
Nachdem ich hier http://www.delphipraxis.net/1318130-post5.html diesen Code gefunden habe
Delphi-Quellcode:
function TForm1.GetSizeAsString(Size: Int64): string;
var
  new: Extended;
  Sign: String;
  c: Integer;
begin
  c:=0;
  new:=Size;
  while new>1024 do
  begin
    new:=new/1024;
    Inc(c);
  end;
  case c of
    0: Sign:=' Byte';
    1: Sign:=' KB';
    2: Sign:=' MB';
    3: Sign:=' GB';
    4: Sign:=' TB';
    5: Sign:=' PB';
    6: Sign:=' EB';
    7: Sign:=' ZB';
    8: Sign:=' YB';
  else
    Sign:=' ('+intToStr(c)+')';
  end;
  Result:=FormatFloat('#,##0.00', new)+Sign;
end;
fielen mir auch wieder meine Sünden und guten Vorsätze ein, die ich jedes mal in einem Gedanken begehe (Sünden) und vornehme (Vorsätze), wenn ich so eine Darstellung der Größe benötige. Schnell was gebaut und gut - schön machen wir es wenn wir Zeit haben ...

Hier einmal für alle, die solche Kleinigkeiten auch immer vor sich herschieben, einen süßen Record Helper der sich um die Darstellung kümmert.
  • Berücksichtigt habe ich dabei auch die unterschiedliche Zählweisen von den Platten-Herstellern (und apple) mit der Basis 1000 und dem Rest der Welt mit der Basis 1024.
  • Der String für die Darstellung wird bis 999,9 YB nie größer als 8 Zeichen breit werden.
Delphi-Quellcode:
type
  TSizeUnit = ( One, Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta );

  TSizeInBytes = type Int64;

  HSizeInBytes = record helper for TSizeInBytes
  type
    TBase = ( f1000, f1024 );

  const
{$IFDEF MACOS}
    DefaultBase = TBase.f1000; // disc manufacturer/apple default
    AltBase = TBase.f1024;
{$ELSE}
    DefaultBase = TBase.f1024; // microsoft default
    AltBase = TBase.f1000;
{$ENDIF}
    BaseFactor: array [ TBase ] of Integer = ( 1000, 1024 );

    ByteSizeFormat: array [ TSizeUnit ] of string = (
      {} ',0 "Byte"',
      {} ',0.0 "KB"',
      {} ',0.0 "MB"',
      {} ',0.0 "GB"',
      {} ',0.0 "TB"',
      {} ',0.0 "PB"',
      {} ',0.0 "EB"',
      {} ',0.0 "ZB"',
      {} ',0.0 "YB"' );

    function ToFloat( const ASizeUnit: TSizeUnit ): Double; overload;
    function ToFloat( const ABase: TBase; const ASizeUnit: TSizeUnit ): Double; overload;

    function ToString( ): string; overload;
    function ToString( const ASizeUnit: TSizeUnit ): string; overload;
    function ToString( const ABase: TBase ): string; overload;
    function ToString( const ABase: TBase; const ASizeUnit: TSizeUnit ): string; overload;
    function ToString( const ABase: TBase; const AFormatSettings: TFormatSettings ): string; overload;
    function ToString( const ABase: TBase; const ASizeUnit: TSizeUnit; const AFormatSettings: TFormatSettings ): string; overload;
  end;

{ HSizeInBytes }

function HSizeInBytes.ToString(
  const ABase : TBase;
  const AFormatSettings: TFormatSettings ): string;
var
  current: TSizeUnit;
  size : Double;
begin
  current := low( TSizeUnit );
  size := Self;
  while ( size >= 1000 ) and ( current < high( TSizeUnit ) ) do
    begin
      size := size / BaseFactor[ ABase ];
      Inc( current );
    end;
  Result := FormatFloat( ByteSizeFormat[ current ], size );
end;

function HSizeInBytes.ToFloat( const ASizeUnit: TSizeUnit ): Double;
begin
  Result := ToFloat( DefaultBase, ASizeUnit )
end;

function HSizeInBytes.ToFloat(
  const ABase : TBase;
  const ASizeUnit: TSizeUnit ): Double;
var
  current: TSizeUnit;
begin
  Result := Self;
  current := low( TSizeUnit );
  while current < ASizeUnit do
    begin
      Result := Result / BaseFactor[ ABase ];
      Inc( current );
    end;
end;

function HSizeInBytes.ToString(
  const ABase : TBase;
  const ASizeUnit : TSizeUnit;
  const AFormatSettings: TFormatSettings ): string;
begin
  Result := FormatFloat( ByteSizeFormat[ ASizeUnit ], ToFloat( ASizeUnit ), AFormatSettings );
end;

function HSizeInBytes.ToString: string;
begin
  Result := ToString( DefaultBase, FormatSettings );
end;

function HSizeInBytes.ToString( const ASizeUnit: TSizeUnit ): string;
begin
  Result := ToString( DefaultBase, ASizeUnit, FormatSettings );
end;

function HSizeInBytes.ToString( const ABase: TBase ): string;
begin
  Result := ToString( ABase, FormatSettings );
end;

function HSizeInBytes.ToString(
  const ABase : TBase;
  const ASizeUnit: TSizeUnit ): string;
begin
  Result := ToString( ABase, ASizeUnit, FormatSettings );
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
 

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:21 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 by Thomas Breitkreuz