ACF Library

DisplayBytes

Back to List

Description: Convert bytes count to short with measurement units.

FileMaker Prototype:

Set Variable [$res; ACF_Run("DisplayBytes"; long_number;  bool_alsoBytes)]

Category: UTILITY

Function source:

function DisplayBytes ( long number, bool alsoBytes )
   string result, unit; 
   float fres; 
   long K=1024; 
   case
      :(abs(number) < K)   // Bytes
         fres = number; 
         unit = "B";
      :(abs(number) < K^2) // Kbytes
         fres = number/K; 
         unit = "KB";
      :(abs(number) < K^3) // Mbytes
         fres = number/K^2; 
         unit = "MB";
      :(abs(number) < K^4) // GBytes
         fres = number/K^3; 
         unit = "GB";
      :(abs(number) < K^5) // TBytes
         fres = number/K^4;
         unit = "TB";
   default
      fres = number/K^4; 
      unit = "TB";
   end case 
   result = format(((unit=="B")?"%.0f":"%.1f"), round(fres,1))+ " "+ unit; 
   if ( alsoBytes ) then
      // Bytes count width 1000-group separators. 
      result += " ("+regex_replace ( "\d{1,3}(?=(\d{3})+(?!\d))", string(number), "$&,")+" Bytes)" ;
   end if
   
   return result; 
end

The DisplayBytes function converts a byte count into a human-readable format with measurement units. It’s especially useful for displaying file sizes, disk capacities, or any value measured in bytes. The second parameter, if set to true, appends the exact byte count in parentheses, formatted with thousand separators.

The function also handles negative byte counts.

Example Usage

Set Variable [$res; ACF_Run("DisplayBytes"; 1347658; 0)]
// Result: 1.3 MB

Set Variable [$res; ACF_Run("DisplayBytes"; 976400223; 0)]
// Result: 931.2 MB

Set Variable [$res; ACF_Run("DisplayBytes"; 976400223775; 0)]
// Result: 909.3 GB

Including the Exact Byte Count

When the second parameter is set to 1, the output includes the exact byte count in parentheses.

Set Variable [$res; ACF_Run("DisplayBytes"; 1347658; 1)]
// Result: 1.3 MB (1,347,658 Bytes)

Set Variable [$res; ACF_Run("DisplayBytes"; 976400223; 1)]
// Result: 931.2 MB (976,400,223 Bytes)

Set Variable [$res; ACF_Run("DisplayBytes"; 976400223775; 1)]
// Result: 909.3 GB (976,400,223,775 Bytes)

Handling Negative Byte Counts

The function can also process negative values:

Set Variable [$res; ACF_Run("DisplayBytes"; -976400223775; 1)]
// Result: -909.3 GB (-976,400,223,775 Bytes)

This versatile function provides clear, human-readable size information, ideal for presenting byte-based measurements in a concise format.

Back to List