Skip to content

Commit 816095d

Browse files
committed
Implement number base
1 parent 5c3427e commit 816095d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/FileSize.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
class FileSize
1515
{
16+
/**
17+
* The number base.
18+
*
19+
* @var int
20+
*/
21+
private $base;
22+
1623
/**
1724
* The number of bytes in this filesize.
1825
*
@@ -32,10 +39,11 @@ class FileSize
3239
*
3340
* @param string|int $size Such as '100 MB'
3441
*/
35-
public function __construct($size = null)
42+
public function __construct($size = null, $base = 2)
3643
{
3744
$this->unitMapper = new UnitMapper();
3845

46+
$this->base = $base;
3947
$this->bytes = $size ? $this->sizeToBytes($size) : 0;
4048
}
4149

@@ -132,7 +140,7 @@ public function as($unitString, $precision = 2)
132140
public function asAuto($precision = 2)
133141
{
134142
$factor = Math::factorByBytes($this->bytes);
135-
$size = $this->bytes / Math::bytesByFactor($factor);
143+
$size = $this->bytes / Math::bytesByFactor($factor, $this->base);
136144
$unit = $this->unitMapper->keyFromIndex($factor);
137145

138146
return self::formatNumber($size, $precision, $unit);
@@ -155,7 +163,8 @@ private function convert($size, $fromUnit, $toUnit, $precision = null)
155163
if ($fromUnit !== $toUnit) {
156164
$index1 = $this->unitMapper->indexFromKey($fromUnit);
157165
$index2 = $this->unitMapper->indexFromKey($toUnit);
158-
$size = (float) $size * Math::bytesByFactor($index1 - $index2);
166+
$factor = $index1 - $index2;
167+
$size = (float) $size * Math::bytesByFactor($factor, $this->base);
159168
}
160169

161170
if ($toUnit === UnitMap::BYTE) {

0 commit comments

Comments
 (0)