Delphi function to display the number of bytes in Windows

It is simple (I think).

Is there a built-in function or a function that someone created that can be called from Delphi that displays several bytes (for example, file size), how does Windows display in the file properties window?

eg. This is what the Windows properties window looks like:

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

The display has the ability to use bytes, KB, MB or GB and shows only 3 significant digits for KB, MB and GB. It then follows that by displaying the exact number of bytes in brackets with commas separating thousands. This is a very nice display, well thought out.

Does anyone know about such a feature?


Edit: I am very surprised that there was no function for this.

Thanks for your helpful ideas. I came up with this that seems to work:

 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; 

This is probably good enough, but is there anything better?

+6
function formatting delphi filesize
source share
3 answers

See the following functions, all in the shlwapi library .

Any of them will provide you with the first part of the desired display format. Check the documentation or write your own tests to confirm that they give you the expected conversions if the megabyte consists of 1000 or 1024 kilobytes. For the second part of your display format, you can start by answering another question:

  • How to convert int to currency? (He did ask how to insert commas, not specifically about money.)

But perhaps this question is the wrong way down, since all the suggestions there, as well as FloatToStrF , fail in the upper limits of Int64 . You will lose a few bytes, but I consider these rather important bytes, since the purpose of the second value in this display format is the exact number.

Once you have all the pieces, stick them together. I am using the hypothetical IntToStrCommas function here, and if you want to implement it as FloatToStrF , go ahead.

 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; 
+12
source share

Not quite what you need, but I have a function in my library that can give you an idea on how to solve this problem:

 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; 
+5
source share

This is from my dzlib module 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, eg 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; 
0
source share

All Articles