diff --git a/src/Memory.php b/src/Memory.php index 764741b..ae19b54 100644 --- a/src/Memory.php +++ b/src/Memory.php @@ -39,7 +39,10 @@ public function size(float|int $size): self */ public function show(): string { - return "$this->preparedSize {$this->units[$this->pow]}"; + $preparedSize = number_format($this->preparedSize, 2); + $trimmed = rtrim(rtrim($preparedSize, '0'), '.'); + + return "$trimmed {$this->units[$this->pow]}"; } /** @@ -82,7 +85,7 @@ protected function initPow(): void */ protected function initPreparedSize(): void { - $this->preparedSize = round($this->size / pow(1024, $this->pow)); + $this->preparedSize = $this->size / pow(1024, $this->pow); } /** @@ -97,4 +100,4 @@ protected function showMemory(string $prefix = ''): string return $this->show(); } -} \ No newline at end of file +} diff --git a/tests/MemoryTest.php b/tests/MemoryTest.php index f8363d3..294a938 100644 --- a/tests/MemoryTest.php +++ b/tests/MemoryTest.php @@ -16,8 +16,11 @@ public static function memories(): array return [ [1, '1 B'], [2048.0, '2 KB'], + [2396.16, '2.34 KB'], [3145728, '3 MB'], + [3869245.44, '3.69 MB'], [4294967296.0, '4 GB'], + [4831838208, '4.5 GB'], ]; } @@ -100,4 +103,4 @@ protected function setUp(): void parent::setUp(); $this->memory = new Memory(); } -} \ No newline at end of file +}