Skip to content

Commit f33e6ed

Browse files
authored
[Feature] Added bit to magnitude helper (alexjustesen#1200)
1 parent 0827861 commit f33e6ed

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

app/Helpers/Number.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66

77
class Number extends SupportNumber
88
{
9+
/**
10+
* Convert the given number to a specific bit order of magnitude.
11+
*/
12+
public static function bitsToMagnitude(int|float $bits, int $precision = 0, string $magnitude = 'kbit'): float
13+
{
14+
$value = match ($magnitude) {
15+
'kbit' => $bits * 1000,
16+
'mbit' => $bits * pow(1000, -2),
17+
'gbit' => $bits * pow(1000, -3),
18+
'tbit' => $bits * pow(1000, -4),
19+
'pbit' => $bits * pow(1000, -5),
20+
'ebit' => $bits * pow(1000, -6),
21+
'zbit' => $bits * pow(1000, -7),
22+
'ybit' => $bits * pow(1000, -8),
23+
default => $bits,
24+
};
25+
26+
return static::format($value, $precision);
27+
}
28+
929
/**
1030
* Convert the given number to its largest bit order of magnitude.
1131
*

0 commit comments

Comments
 (0)