kartik\helpers\Enum::formatBytes PHP Method

formatBytes() public static method

Example: ~~~ echo Enum::formatBytes(120.32); // returns: 1.17 KB echo Enum::formatBytes(28434322.25); // returns: 27.12 MB echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB ~~~
public static formatBytes ( double $bytes, integer $precision = 2 ) : string
$bytes double number of bytes
$precision integer the number of decimal places to round off
return string
    public static function formatBytes($bytes, $precision = 2)
    {
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
        $bytes = max($bytes, 0);
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
        $pow = min($pow, count($units) - 1);
        $bytes /= pow(1024, $pow);
        return round($bytes, $precision) . ' ' . $units[$pow];
    }