Skip to content

Commit fb9e263

Browse files
committed
Merge pull request php-debugbar#246 from darwish/format-negative-bytes
Added support for negative numbers in DataFormatter::formatBytes()
2 parents 9d2bc8d + b1f5e65 commit fb9e263

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/DebugBar/DataFormatter/DataFormatter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ public function formatBytes($size, $precision = 2)
7070
if ($size === 0 || $size === null) {
7171
return "0B";
7272
}
73+
74+
$sign = $size < 0 ? '-' : '';
75+
$size = abs($size);
76+
7377
$base = log($size) / log(1024);
7478
$suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
75-
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
79+
return $sign . round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
7680
}
7781
}

tests/DebugBar/Tests/DataFormatter/DataFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ public function testFormatBytes()
2929
$this->assertEquals("1B", $f->formatBytes(1));
3030
$this->assertEquals("1KB", $f->formatBytes(1024));
3131
$this->assertEquals("1MB", $f->formatBytes(1024 * 1024));
32+
$this->assertEquals("-1B", $f->formatBytes(-1));
33+
$this->assertEquals("-1KB", $f->formatBytes(-1024));
34+
$this->assertEquals("-1MB", $f->formatBytes(-1024 * 1024));
3235
}
3336
}

0 commit comments

Comments
 (0)