Number.php
public static function byteFormat(int $bytes = 0, int $decimals = 0)
    {
        $quant = [
            'TB' => 1099511627776,  // pow( 1024, 4)
            'GB' => 1073741824,     // pow( 1024, 3)
            'MB' => 1048576,        // pow( 1024, 2)
            'KB' => 1024,           // pow( 1024, 1)
            'B ' => 1,              // pow( 1024, 0)
        ];

        foreach ($quant as $unit => $mag ) {
            if (doubleval($bytes) >= $mag) {
                return sprintf('%01.'.$decimals.'f', ($bytes / $mag)).' '.$unit;
            }
        }

        return false;
    }
public static function byteFormat(int $bytes = 0, int $decimals = 0)
    {
        $quant = [
            'TB' => 1099511627776,  // pow( 1024, 4)
            'GB' => 1073741824,     // pow( 1024, 3)
            'MB' => 1048576,        // pow( 1024, 2)
            'KB' => 1024,           // pow( 1024, 1)
            'B ' => 1,              // pow( 1024, 0)
        ];

        foreach ($quant as $unit => $mag ) {
            if (doubleval($bytes) >= $mag) {
                return sprintf('%01.'.$decimals.'f', ($bytes / $mag)).' '.$unit;
            }
        }

        return false;
    }
public static function byteFormat ( int $ bytes = 0 , int $ decimals = 0 ) { $ quant = [ 'TB' => 1099511627776 , // pow( 1024, 4) 'GB' => 1073741824 , // pow( 1024, 3) 'MB' => 1048576 , // pow( 1024, 2) 'KB' => 1024 , // pow( 1024, 1) 'B ' => 1 , // pow( 1024, 0) ] ; foreach ( $ quant as $ unit => $ mag ) { if ( doubleval ( $ bytes ) >= $ mag ) { return sprintf ( '%01.' . $ decimals . 'f' , ( $ bytes / $ mag ) ) . ' ' . $ unit ; } } return false ; }
Converts a number of bytes to a human readable number by taking the
number of that unit that the bytes will go into it.

echo Num::format_bytes('204800');     // 200 kB
echo Num::format_bytes('214901', 1);  // 209.9 kB
echo Num::format_bytes('2249010', 1); // 2.1 MB
echo Num::format_bytes('badbyteshere'); // false

@param  int  $bytes    The number to format.
@param  int  $decimals The number of decimals to round up to.
@return boolean|string
Converts a number of bytes to a human readable number by taking the number of that unit that the bytes will go into it .