6

これは単純なものです(私は思います)。

Windowsがファイルの[プロパティ]ボックスに表示する方法であるバイト数(ファイルサイズなど)を表示するシステム組み込み関数、またはDelphiから呼び出すことができる誰かが作成した関数はありますか?

例:これは、Windowsのプロパティボックスにさまざまなサイズを表示する方法です。

539 bytes (539 bytes)
35.1 KB (35,974 bytes)
317 MB (332,531,365 bytes)
2.07 GB (2,224,617,077 bytes)

ディスプレイは、バイト、KB、MB、またはGBの使用についてスマートであり、KB、MB、およびGBの有効数字は3桁のみです。次に、括弧内に正確なバイト数を表示し、数千をコンマで区切ります。よく考えられたとても素敵なディスプレイです。

誰かがそのような機能を知っていますか?


編集:私はこれのための機能がなかったことに非常に驚いています。

有益なアイデアをありがとう。私はこれを思いついた、それはうまくいくようだ:

function BytesToDisplay(A:int64): string;
var
  A1, A2, A3: double;
begin
  A1 := A / 1024;
  A2 := A1 / 1024;
  A3 := A2 / 1024;
  if A1 < 1 then Result := floattostrf(A, ffNumber, 15, 0) + ' bytes'
  else if A1 < 10 then Result := floattostrf(A1, ffNumber, 15, 2) + ' KB'
  else if A1 < 100 then Result := floattostrf(A1, ffNumber, 15, 1) + ' KB'
  else if A2 < 1 then Result := floattostrf(A1, ffNumber, 15, 0) + ' KB'
  else if A2 < 10 then Result := floattostrf(A2, ffNumber, 15, 2) + ' MB'
  else if A2 < 100 then Result := floattostrf(A2, ffNumber, 15, 1) + ' MB'
  else if A3 < 1 then Result := floattostrf(A2, ffNumber, 15, 0) + ' MB'
  else if A3 < 10 then Result := floattostrf(A3, ffNumber, 15, 2) + ' GB'
  else if A3 < 100 then Result := floattostrf(A3, ffNumber, 15, 1) + ' GB'
  else Result := floattostrf(A3, ffNumber, 15, 0) + ' GB';
  Result := Result + ' (' + floattostrf(A, ffNumber, 15, 0) + ' bytes)';
end;

これで十分かもしれませんが、もっと良いものはありますか?

4

3 に答える 3

12

すべてshlwapiライブラリにある次の関数を参照してください。

それらのいずれかが、希望する表示形式の最初の部分を提供します。ドキュメントを確認するか、独自のテストを作成して、メガバイトが1000キロバイトと1024キロバイトのどちらで構成されているかに関して期待する変換が得られることを確認してください。表示形式の2番目の部分では、別のStackOverflowの質問への回答から始めることができます。

しかし、おそらくその質問は、そこにあるすべての提案とFloatToStrF、の上限で失敗するため、下がるのは間違った道ですInt64。数バイトが失われますが、その表示形式の2番目の値の目的は正確な数値を提供することであるため、これらの非常に重要なバイトを考慮します。

すべての部品が揃ったら、それらを接着します。ここでは架空のIntToStrCommas関数を使用しています。それをとして実装する場合はFloatToStrF、先に進んでください。

function BytesToDisplay(const num: Int64): string;
var
  // If GB is the largest unit available, then 20 characters is
  // enough for "17,179,869,183.99 GB", which is MaxUInt64.
  buf: array[0..20] of Char;
begin
  if StrFormatByteSize64(num, buf, Length(buf)) = nil then
    raise EConvertError.CreateFmt('Error converting %d', [num]);
  Result := Format('%s (%s bytes)', [buf, IntToStrCommas(num)]);
end;
于 2009-08-17T03:23:17.897 に答える
5

正確にはあなたが求めているものではありませんが、私のライブラリには、これに取り組む方法のアイデアを与える可能性のある関数があります。

function FormatByteSize(const bytes: Longword): string;
var
  B: byte;
  KB: word;
  MB: Longword;
  GB: Longword;
  TB: UInt64;
begin

  B  := 1; //byte
  KB := 1024 * B; //kilobyte
  MB := 1000 * KB; //megabyte
  GB := 1000 * MB; //gigabyte
  TB := 1000 * GB; //teraabyte

  if bytes > TB then
    result := FormatFloat('#.## TB', bytes / TB)
  else
    if bytes > GB then
      result := FormatFloat('#.## GB', bytes / GB)
    else
      if bytes > MB then
        result := FormatFloat('#.## MB', bytes / MB)
      else
        if bytes > KB then
          result := FormatFloat('#.## KB', bytes / KB)
        else
          result := FormatFloat('#.## bytes', bytes) ;
end;
于 2009-08-17T03:01:33.023 に答える
0

これは私のdzlibユニットu_dzConvertUtilsからのものです:

/// these contants refer to the "Xx binary byte" units as defined by the
/// International Electronical Commission (IEC) and endorsed by the
/// IEE and CiPM </summary>
const
  OneKibiByte = Int64(1024);
  OneMebiByte = Int64(1024) * OneKibiByte;
  OneGibiByte = Int64(1024) * OneMebiByte;
  OneTebiByte = Int64(1024) * OneGibiByte;
  OnePebiByte = Int64(1024) * OneTebiByte;
  OneExbiByte = Int64(1024) * OnePebiByte;

/// <summary>
/// Converts a file size to a human readable string, e.g. 536870912000 = 5.00 GiB (gibibyte) </summary>
function FileSizeToHumanReadableString(_FileSize: Int64): string;
begin
  if _FileSize > 5 * OneExbiByte then
    Result := Format(_('%.2f EiB'), [_FileSize / OneExbiByte])
  else if _FileSize > 5 * OnePebiByte then
    Result := Format(_('%.2f PiB'), [_FileSize / OnePebiByte])
  else if _FileSize > 5 * OneTebiByte then
    Result := Format(_('%.2f TiB'), [_FileSize / OneTebiByte])
  else if _FileSize > 5 * OneGibiByte then
    Result := Format(_('%.2f GiB'), [_FileSize / OneGibiByte])
  else if _FileSize > 5 * OneMebiByte then
    Result := Format(_('%.2f MiB'), [_FileSize / OneMebiByte])
  else if _FileSize > 5 * OneKibiByte then
    Result := Format(_('%.2f KiB'), [_FileSize / OneKibiByte])
  else
    Result := Format(_('%d Bytes'), [_FileSize]);
end;
于 2015-06-01T12:40:10.263 に答える