Skip to content

Commit 65e3756

Browse files
committed
Added optional precision argument to format()
1 parent 00c8a00 commit 65e3756

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Byte/ByteFormatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ public function __construct(UnitDecorator $unitDecorator = null)
3434
;
3535
}
3636

37-
public function format($bytes)
37+
public function format($bytes, $precision = null)
3838
{
39+
$precision = $precision === null ? $this->precision : $precision;
3940
$log = log($bytes, $this->base);
4041
$exponent = max(0, $log|0);
41-
$value = round(pow($this->base, $log - $exponent), $this->precision);
42+
$value = round(pow($this->base, $log - $exponent), $precision);
4243
$units = $this->getUnitDecorator()->decorate($exponent, $this->base, $value);
4344

4445
return trim(sprintf($this->normalizedFormat, $value, $units));

test/Integration/ByteFormatterTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ public function provideDecimalIntegers()
7777
/** @dataProvider providePrecisionIntegers */
7878
public function testPrecision($integer, $formatted)
7979
{
80-
$this->assertSame($formatted, $this->formatter->setPrecision(2)->format($integer));
80+
$withArgument = $this->formatter->setPrecision(5)->format($integer, 2);
81+
$withMethod = $this->formatter->setPrecision(2)->format($integer);
82+
83+
$this->assertSame($formatted, $withMethod);
84+
$this->assertSame($withMethod, $withArgument);
8185
}
8286

8387
public function providePrecisionIntegers()

0 commit comments

Comments
 (0)