Skip to content

Commit

Permalink
update for decimal pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
tkachikov committed May 26, 2023
1 parent fca11b1 commit 93991ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]}";
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -97,4 +100,4 @@ protected function showMemory(string $prefix = ''): string

return $this->show();
}
}
}
5 changes: 4 additions & 1 deletion tests/MemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

Expand Down Expand Up @@ -100,4 +103,4 @@ protected function setUp(): void
parent::setUp();
$this->memory = new Memory();
}
}
}

0 comments on commit 93991ba

Please sign in to comment.