webCOMAND

string_util::format_bytes()

Format a number as a number of bytes, kilobytes, megabytes, etc.

Prototype

string format_bytes(integer $num_bytes, integer $precision = 2)

Parameters

  • num_bytes - Number of bytes to format.
  • precision - Number of decimal places to show, and round to, if necessary.  Default is 2, for two decimal places.

Return

Text string of number of bytes (bytes), kilobytes (KB), megabytes (MB), etc. in the format that looks like "12.25 KB".

Example

// output: 3.25 KB
$size = 1024 * 3250;
$formatted_size = \io_comand_util\string_util::format_bytes($size, 3);
echo($formatted_size);