Skip to content

Commit 4c2f37a

Browse files
committed
Merge pull request #4 from okdana/precision
Add optional precision argument to format()
2 parents 00c8a00 + 4fc26ca commit 4c2f37a

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ By default all values are rounded to the nearest integer.
4141
```
4242
> 513 KiB
4343
44-
Increasing the precision with `setPrecision($precision)` allows the specified amount of digits after the decimal point.
44+
The default precision can be increased with `setPrecision($precision)`. Increasing the precision allows the specified amount of digits after the decimal point.
45+
4546
```php
4647
(new ByteFormatter)->setPrecision(2)->format(0x80233);
4748
```
@@ -55,6 +56,13 @@ needed.
5556
```
5657
> 512.5 KiB
5758
59+
The default precision can be overridden on a per-format basis using the optional precision argument to `format()`.
60+
61+
```php
62+
(new ByteFormatter)->setPrecision(2)->format(0x80233, 4);
63+
```
64+
> 512.5498 KiB
65+
5866
Output format
5967
-------------
6068

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function provideDecimalIntegers()
7878
public function testPrecision($integer, $formatted)
7979
{
8080
$this->assertSame($formatted, $this->formatter->setPrecision(2)->format($integer));
81+
$this->assertSame($formatted, $this->formatter->setPrecision(5)->format($integer, 2));
8182
}
8283

8384
public function providePrecisionIntegers()

test/Integration/DocumentationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function testPrecision()
2222
$this->assertSame('512.55 KiB', (new ByteFormatter)->setPrecision(2)->format(0x80233));
2323

2424
$this->assertSame('512.5 KiB', (new ByteFormatter)->setPrecision(2)->format(0x80200));
25+
26+
$this->assertSame('512.5498 KiB', (new ByteFormatter)->setPrecision(2)->format(0x80233, 4));
2527
}
2628

2729
public function testOutputFormat()

0 commit comments

Comments
 (0)