문제

이것은 간단한 것입니다 (제 생각에는).

기능이 내장 된 시스템 또는 델파이에서 호출 할 수있는 누군가가 만든 기능이 있습니까?

예를 들어 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;

이것은 아마도 충분하지만 더 좋은 것이 있습니까?

도움이 되었습니까?

해결책

다음 기능을 모두 참조하십시오 Shlwapi 도서관.

그들 중 누구라도 원하는 디스플레이 형식의 첫 번째 부분을 줄 것입니다. 문서를 확인하거나 자신의 테스트를 작성하여 메가 바이트가 1000 또는 1024 킬로 바이트로 구성되어 있는지 여부와 관련하여 기대하는 전환을 확인하십시오. 디스플레이 형식의 두 번째 부분의 경우 다른 스택 오버 플로우 질문에 대한 답으로 시작할 수 있습니다.

그러나 아마도 그 질문은 모든 제안과뿐만 아니라 FloatToStrF, 상한에서 실패합니다 Int64. 몇 바이트를 잃을 것입니다. 그러나 해당 디스플레이 형식의 두 번째 값의 목적은 정확한 숫자를 제공하는 것이기 때문에 매우 중요한 바이트를 고려합니다.

모든 조각이 있으면 함께 붙입니다. 나는 가설을 사용하고 있습니다 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;

다른 팁

정확히 당신이 따르는 것이 아니라 내 라이브러리에 이것을 다루는 방법을 알 수있는 기능이 있습니다.

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;

이것은 내 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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top