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

@param   integer $bytes    O número com medida ex: 1T
@param   integer $decimals O tipo de saída
@return  string
426431
function convertMemorySize($strval, string $to_unit = 'b'){
        $strval    = strtolower(str_replace(' ', '', $strval));
        $val       = floatval($strval);
        $to_unit   = strtolower(trim($to_unit))[0];
        $from_unit = str_replace($val, '', $strval);
        $from_unit = empty($from_unit) ? 'b' : trim($from_unit)[0];
        $units     = 'kmgtph';  // (k)ilobyte, (m)egabyte, (g)igabyte and so on...

        // Convert to bytes
        if ($from_unit !== 'b'){
            $val *= 1024 ** (strpos($units, $from_unit) + 1);
        }

        // Convert to unit
        if ($to_unit !== 'b'){
            $val /= 1024 ** (strpos($units, $to_unit) + 1);
        }

        $val = $val.' '.($to_unit!='b'?strtoupper($to_unit).'B':'B');

        return $val;
    }
445366
public function formatBytes($bytes, $dec = 2)
    {
        $unitpow = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        $pos = 0;
        while ($bytes >= 1024) {
            $bytes /= 1024;
            ++$pos;
        }

        return round($bytes, $dec).' '.$unitpow[$pos];
    }
90399
private function formatBytes($size)
    {
        if ($size === 0) {
            return 0;
        }
        $base = log($size, 1024);
        $suffixes = [ 'bytes', 'k', 'M', 'G', 'T' ];

        $floor = floor($base);
        return round(pow(1024, ($base - $floor)), 2).' '.$suffixes[$floor];
    }
438399
public static function filesize($filename, $decimals = 2)
    {
        $bytes = filesize($filename);
        $factor = floor((strlen($bytes) - 1) / 3);
        $size = 'BKMGTP';
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
    }
1167899
public static function filesize($bytes)
    {
        $precision = (($bytes % 1024) === 0 || $bytes <= 1024) ? 0 : 1;

        $formatter = new ByteSize\Formatter\Binary();
        $formatter->setPrecision($precision);

        return $formatter->format($bytes);
    }
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);
    }
875341
public static function formatToDecimal($value, $decimals = 2)
    {
        if (trim($value) != null) {
            return number_format($value, $decimals, '.', '');
        }
        return null;
    }
681092
public static function formatByteSize( $size, $precision = 1, $symbols = NULL )
    {
        if ( $symbols === NULL ){
            $symbols = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
        }
        $i=0;
        while (($size/1024)>1) {
            $size=$size/1024;
            $i++;
        }
        return (round($size,$precision)." ".$symbols[$i]);
    }
117282
public function number($number, $decimals = null)
    {
        return number_format(
            (float) $number,
            $decimals,
            $this->getSymbol(self::DECIMAL_MARK),
            $this->getSymbol(self::THOUSANDS_SEPARATOR)
        );
    }
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;
    }
423486
public static function readableSize($bytes, $precision = 1)
    {
        static $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        if ($bytes === 0) {
            return '0 '.$units[0];
        }
        $exponent = floor(log($bytes, 1024));
        $value = $bytes / pow(1024, $exponent);

        return round($value, $precision).' '.$units[$exponent];
    }
616642
public static function formatBytes($size, $precision = 0, $format = 'decimal')
    {
        if (!array_key_exists($format, self::$formats)) {
            throw new \InvalidArgumentException('Not a valid format');
        }

        $format = self::$formats[$format];
        $precision = (int) $precision;

        /** @var float $base */
        $base = log((float) $size, $format['mod']);
        $key = (int) floor($base);

        $value = round(pow($format['mod'], $base - floor($base)), $precision);

        return sprintf('%.' . $precision . 'f %s', $value, $format['units'][$key]);
    }
1319061
public function formatStandardNumber($number, $decimals = null)
    {
        $lang = $this->request->getSession()->getLang();

        if ($decimals === null) {
            $decimals = $lang->getDecimals();
        }

        return number_format($number, $decimals, '.', '');
    }
196553
public static function convertToHumanReadable($bytes, $decimals = 2)
    {
        $factor = floor((strlen($bytes) - 1) / 3);

        $value = sprintf("%.{$decimals}f", $bytes / pow(1024, $factor));
        $unit = static::$sizes[(string)$factor];

        return sprintf('%g %s', $value, $unit);
    }
803347
private function formatSize($bytes)
    {
        $unit = 1024;
        if ($bytes <= $unit) {
            return $bytes.' b';
        }
        $exp = (int)(log($bytes) / log($unit));
        $pre = 'kMGTPE';
        $pre = $pre[$exp - 1];

        return sprintf('%.1f %sB', $bytes / pow($unit, $exp), $pre);
    }
618623
public static function nFloat($number, $decimals = 2, $showThousands = false)
    {
        if (is_null($number) || empty(self::onlyNumbers($number)) || floatval($number) == 0) {
            return 0;
        }
        $pontuacao = preg_replace('/[0-9]/', '', $number);
        $locale = (mb_substr($pontuacao, -1, 1) == ',') ? "pt-BR" : "en-US";
        $formater = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);

        if ($decimals === false) {
            $decimals = 2;
            preg_match_all('/[0-9][^0-9]([0-9]+)/', $number, $matches);
            if (!empty($matches[1])) {
                $decimals = mb_strlen(rtrim($matches[1][0], 0));
            }
        }

        return number_format($formater->parse($number, \NumberFormatter::TYPE_DOUBLE), $decimals, '.', ($showThousands ? ',' : ''));
    }
1272148
public function format($bytes, $precision = null)
    {
        // Use default precision when not specified.
        $precision === null && $precision = $this->getPrecision();

        $log = log($bytes, $this->getBase());
        $exponent = $this->hasFixedExponent() ? $this->getFixedExponent() : max(0, $log|0);
        $value = round(pow($this->getBase(), $log - $exponent), $precision);
        $units = $this->getUnitDecorator()->decorate($exponent, $this->getBase(), $value);

        return trim(sprintf($this->sprintfFormat, $this->formatValue($value, $precision), $units));
    }