* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShop\PrestaShop\Core\Util\File; /** * Converts int value to formatted file size value */ class FileSizeConverter { /** * @param int $bytes * @param int $precision * * @return string */ public function convert(int $bytes, int $precision = 2): string { $units = ['B', 'kB', 'MB', 'GB', 'TB']; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); if ($bytes >= 1024) { $bytes /= pow(1024, $pow); $bytes = number_format(round($bytes, $precision), 2, '.', ''); } return $bytes . $units[$pow]; } }