加入bit转换功能

This commit is contained in:
Chenx221 2024-04-13 18:18:30 +08:00
parent 54459f0472
commit 992e17a7d2
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021

View File

@ -123,13 +123,37 @@ class FileSizeHelper
{ {
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$bytes = max($bytes, 0); return self::formatProcess($bytes, $units, $precision);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); }
/**
* @param $bits
* @param $precision
* @param $sp
* @return string
*/
public static function formatBits($bits, $precision = 2, $sp = false): string
{
$units = array('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb');
return self::formatProcess($bits, $units, $precision,$sp?1000:1024);
}
/**
* @param $bits
* @param array $units
* @param mixed $precision
* @return string
*/
private static function formatProcess($bits, array $units, mixed $precision, int $n = 1024): string
{
$bits = max($bits, 0);
$pow = floor(($bits ? log($bits) : 0) / log($n));
$pow = min($pow, count($units) - 1); $pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow); $bits /= pow($n, $pow);
return round($bytes, $precision) . ' ' . $units[$pow]; return round($bits, $precision) . ' ' . $units[$pow];
} }
/** /**
@ -206,4 +230,5 @@ class FileSizeHelper
return max(0, (int)$valueInMB); return max(0, (int)$valueInMB);
} }
} }