<?php declare(strict_types = 1);

namespace Dms\Web\Laravel\Util;

/**
 * The file size formatter
 *
 * @author Elliot Levin <elliotlevin@hotmail.com>
 */
class FileSizeFormatter
{
    /**
     * @param int $size
     * @param int $precision
     *
     * @return string
     */
    public static function formatBytes(int $size, int $precision = 2) : string
    {
        $base     = log($size, 1024);
        $suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];

        return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[(int)floor($base)];
    }
}
<?php declare(strict_types = 1);

namespace Dms\Web\Laravel\Util;

/**
 * The file size formatter
 *
 * @author Elliot Levin <elliotlevin@hotmail.com>
 */
class FileSizeFormatter
{
    /**
     * @param int $size
     * @param int $precision
     *
     * @return string
     */
    public static function formatBytes(int $size, int $precision = 2) : string
    {
        if ($size === 0) {
            return '0B';
        }

        $base     = log($size, 1024);
        $suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];

        return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[(int)floor($base)];
    }
}
Fix issue with formatting 0 bytes file size
Fix issue with formatting 0 bytes file size
