Skip to content

Commit 6975d31

Browse files
committed
add Numbers
1 parent e3f6641 commit 6975d31

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/Numbers.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Php;
4+
5+
final class Numbers
6+
{
7+
8+
public static function minMax(int $value, int $min, int $max): int
9+
{
10+
return min(max($value, $min), $max);
11+
}
12+
13+
public static function bytes(float $bytes, int $precision = 2, bool $withSpace = true): string
14+
{
15+
$bytes = round($bytes);
16+
$units = ['B', 'kB', 'MB', 'GB', 'TB', $end = 'PB'];
17+
18+
foreach ($units as $unit) {
19+
if (abs($bytes) < 1024 || $unit === $end) {
20+
break;
21+
}
22+
23+
$bytes /= 1024;
24+
}
25+
26+
return round($bytes, $precision) . ($withSpace ? ' ' : '') . $unit;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)