Skip to content

Commit 5c3427e

Browse files
committed
Implement number base into Math
1 parent 996c834 commit 5c3427e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/FileSize/Math/Math.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
66

77
namespace ChrisUllyott\FileSize\Math;
88

9+
use ChrisUllyott\FileSize\Exception\FileSizeException;
10+
911
class Math
1012
{
1113
/**
12-
* Get the number of bytes per factor. (2^10 = 1,024; 2^20 = 1,048,576...)
14+
* Get the number of bytes per factor. In base 2, the first factor is 1024,
15+
* while in decimal it is 1000.
1316
*
1417
* @param int $factor
1518
* @return int
1619
*/
17-
public static function bytesByFactor($factor)
20+
public static function bytesByFactor($factor, $base)
1821
{
19-
return 2 ** (10 * $factor);
22+
if ($base === 2) {
23+
return $base ** (10 * $factor);
24+
} elseif ($base === 10) {
25+
return $base ** (3 * $factor);
26+
}
27+
28+
throw new FileSizeException('Invalid number base (use either 2 or 10)');
2029
}
2130

2231
/**

0 commit comments

Comments
 (0)