forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber.php
More file actions
25 lines (20 loc) · 785 Bytes
/
Number.php
File metadata and controls
25 lines (20 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
namespace App\Helpers;
use Illuminate\Support\Number as SupportNumber;
class Number extends SupportNumber
{
/**
* Convert the given number to its file size equivalent in bits.
*/
public static function fileSizeBits(int|float $bits, int $precision = 0, ?int $maxPrecision = null, bool $perSecond = false): string
{
$units = match ($perSecond) {
true => ['Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'],
default => ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb']
};
for ($i = 0; ($bits / 1000) > 0.9 && ($i < count($units) - 1); $i++) {
$bits /= 1000;
}
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
}
}