Formatação agradável para tamanhos de computador (Bytes).

@param   integer $bytes    O número em bytes para formatar
@param   integer $decimals O número de pontos decimais a incluir
@return  string
766011
public static function sizeFormat($bytes, $decimals = 0){
        $bytes = floatval($bytes);
        if ($bytes < 1024) {
            return number_format($bytes, $decimals, '.', '') . ' B';
        } elseif ($bytes < pow(1024, 2)) {
            return number_format($bytes / 1024, $decimals, '.', '') . ' KB';
        } elseif ($bytes < pow(1024, 3)) {
            return number_format($bytes / pow(1024, 2), $decimals, '.', '') . ' MB';
        } elseif ($bytes < pow(1024, 4)) {
            return number_format($bytes / pow(1024, 3), $decimals, '.', '') . ' GB';
        } elseif ($bytes < pow(1024, 5)) {
            return number_format($bytes / pow(1024, 4), $decimals, '.', '') . ' TB';
        } elseif ($bytes < pow(1024, 6)) {
            return number_format($bytes / pow(1024, 5), $decimals, '.', '') . ' PB';
        } else {
            return number_format($bytes / pow(1024, 5), $decimals, '.', '') . ' PB';
        }
    }
245837
public static function byte_format($size, $dec = 2)
    {
        $a   = array("B", "KB", "MB", "GB", "TB", "PB");
        $pos = 0;
        while ($size >= 1024) {
            $size /= 1024;
            $pos++;
        }
        return round($size, $dec) . " " . $a[$pos];
    }
1186382
public function numberFormat($decimals = 0, $thousandSeparator = '.', $decimalSeparator = ',')
    {
        $decimalSeparator = $thousandSeparator === '.' ? ',' : '.';

        return number_format($this->amount, $decimals, $decimalSeparator, $thousandSeparator);
    }
489675
public static function bytesFormat($size, $precision = 2)
    {
        $base = log($size) / log(1024);
        $suffix = array('', 'KB', 'MB', 'GB', 'TB');

        return round(pow(1024, $base - floor($base)), $precision) . $suffix[floor($base)];
    }
834030
public static function quantity(int $num, int $decimals = 0) : string
    {
        if ($num >= 1000 && $num < 1000000) {
            return sprintf('%01.'.$decimals.'f', (sprintf('%01.0f', $num) / 1000)).'K';
        } elseif ($num >= 1000000 && $num < 1000000000) {
            return sprintf('%01.'.$decimals.'f', (sprintf('%01.0f', $num) / 1000000)).'M';
        } elseif ($num >= 1000000000) {
            return sprintf('%01.'.$decimals.'f', (sprintf('%01.0f', $num) / 1000000000)).'B';
        }

        return $num;
    }
1103681
public function memoryToHuman($bytes, $unit = "", $decimals = 2)
    {
        if($bytes <= 0)
            return '0.00 KB';

        $units = [
            'B' => 0,
            'KB' => 1,
            'MB' => 2,
            'GB' => 3,
            'TB' => 4,
            'PB' => 5,
            'EB' => 6,
            'ZB' => 7,
            'YB' => 8
        ];

        $value = 0;
        if ($bytes > 0)
        {
            // Generate automatic prefix by bytes
            // If wrong prefix given
            if ( ! array_key_exists($unit, $units))
            {
                $pow = floor(log($bytes) / log(1024));
                $unit = array_search($pow, $units);
            }

            // Calculate byte value by prefix
            $value = ($bytes / pow(1024, floor($units[$unit])));
        }

        // If decimals is not numeric or decimals is less than 0
        if ( ! is_numeric($decimals) || $decimals < 0)
            $decimals = 2;

        // Format output
        return sprintf('%.' . $decimals . 'f ' . $unit, $value);
    }
979379
public static function format($value, int $decimals = 2): ?string {
        if (!\is_numeric($value)) {
            return null;
        }
        return \number_format($value, $decimals);
    }
619162
public static function humanize(
        $bytes,
        int $decimals = 2,
        array $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    ): string
    {
        $power = 0;
        $temp = $bytes;
        $count = (count($sizes) - 1);
        for ($i = 0; $i < $count; $i++) {
            if ($temp < 1024) {
                break;
            } else {
                $temp /= 1024;
                $power++;
            }
        }

        $humanize = sprintf("%.{$decimals}f", ($bytes / pow(1024, $power))) . $sizes[$power];

        return $humanize;
    }
1371589
public static function FormatFileSize($Bytes, $Precision = 1) {
		$Units = array('B', 'K', 'M', 'G', 'T');

		$Bytes = max((int)$Bytes, 0);
		$Pow = floor(($Bytes ? log($Bytes) : 0) / log(1024));
		$Pow = min($Pow, count($Units) - 1);

		$Bytes /= pow(1024, $Pow);

		$Result = round($Bytes, $Precision).$Units[$Pow];
		return $Result;
	}
236190
public static function byte(Float $bytes, Int $precision = 1, Bool $unit = true) : String
    {
        $byte   = 1024;
        $kb     = 1024 * $byte;
        $mb     = 1024 * $kb;
        $gb     = 1024 * $mb;
        $tb     = 1024 * $gb;
        $pb     = 1024 * $tb;
        $eb     = 1024 * $pb;

        if( $bytes <= $byte && $bytes > -1 )
        {
            $un     = 'B';
            $return = $bytes;
        }
        elseif( $bytes <= $kb && $bytes > $byte )
        {
            $un     = 'KB';
            $return = round(($bytes / $byte),$precision);
        }
        elseif( $bytes <= $mb && $bytes > $kb )
        {
            $un     = 'MB';
            $return = round(($bytes / $kb),$precision);
        }
        elseif( $bytes <= $gb && $bytes > $mb )
        {
            $un     = 'GB';
            $return = round(($bytes / $mb),$precision);
        }
        elseif( $bytes <= $tb && $bytes > $gb )
        {
            $un     = 'TB';
            $return = round(($bytes / $gb),$precision);
        }
        elseif( $bytes <= $pb && $bytes > $tb )
        {
            $un     = 'PB';
            $return = round(($bytes / $tb),$precision);
        }
        elseif( $bytes <= $eb && $bytes > $pb )
        {
            $un     = 'EB';
            $return = round(($bytes / $pb),$precision);
        }
        else
        {
            $un     = 'B';
            $return = str_replace(",", ".", number_format($bytes));
        }

        return $return . ( ! empty($unit) ? $un : NULL );
    }
874278
public static function bytesToString(int $bytes, int $round = 0): string
    {
        return self::getMeasure($bytes, 0, $round);
    }
515525
private function number_format($number, $decimal_precision = 0, $decimals_separator = '.', $thousands_separator = ',') {
        $number = explode('.', str_replace(' ', '', $number));
        $number[0] = str_split(strrev($number[0]), 3);
        $total_segments = count($number[0]);
        for ($i = 0; $i < $total_segments; $i++) {
            $number[0][$i] = strrev($number[0][$i]);
        }
        $number[0] = implode($thousands_separator, array_reverse($number[0]));
        if (!empty($number[1])) {
           $number[1] = round($number[1], $decimal_precision);
        }
        return implode($decimals_separator, $number);
    }
1078746
public function format($decimals = 2, $decPoint = '.', $thousandSep = ',')
    {
        return number_format($this->value, $decimals, $decPoint, $thousandSep);
    }
271097
public function formatFilesize($size, $decimals = 0, $binarySuffix = false)
    {
        $size = (float) ceil($size);

        if (!$size) {
            return 0;
        }

        if (!$binarySuffix) {
            $divisor = 1000;
            $suffixes = array('Byte', 'kB', 'MB', 'GB', 'TB', 'PB');
        } else {
            $divisor = 1024;
            $suffixes = array('Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
        }

        $last = end($suffixes);

        foreach ($suffixes as $suffix) {
            if ($size < $divisor) {
                break;
            }

            if ($suffix === $last) {
                break;
            }

            $size /= $divisor;
        }

        if ($decimals && $suffix !== $suffixes[0]) {
            $locale = localeconv();
            $decPoint = $locale['decimal_point'];
            $thousandsSep = $locale['thousands_sep'];
            $size = number_format($size, $decimals, $decPoint, $thousandsSep);
        } else {
            $size = ceil($size);
        }

        return sprintf('%s %s', $size, $suffix);
    }
204417
private function formatBytes($bytes)
    {
        if ($bytes === 0) {
            return 0;
        }

        $units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
        $base  = log($bytes, 1024);
        $floor = floor($base);
        $unit  = $units[$floor];
        $size  = round(pow(1024, ($base - $floor)), 2);

        $locale = localeconv();
        $size   = number_format($size, 2, $locale['decimal_point'], $locale['thousands_sep']);

        return rtrim($size, '.0').' '.$unit;
    }
958326
public static function formatByte($byte)
    {
        // ignore "Warning: Division by zero"
        if ($byte == 0) {
            return '0 B';
        }
        $s = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
        $e = floor(log($byte)/log(1024));
        return sprintf('%.2f '.$s[$e], ($byte/pow(1024, floor($e))));
    }
246600
protected function formatBytes($size, $precision = 2)
    {
        $base = log($size, 1024);
        $suffixes = array('', 'K', 'M', 'G', 'T');

        return round(pow(1024, $base - floor($base)), $precision) . '' . $suffixes[floor($base)];
    }