Skip to content

Commit

Permalink
Fix number rounding (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Dec 7, 2023
1 parent 77b2de2 commit 5f63272
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions resources/views/livewire/cache.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</div>
<div class="flex flex-col justify-center @sm:block">
<span class="text-xl uppercase font-bold text-gray-700 dark:text-gray-300 tabular-nums">
{{ round($allCacheInteractions->hits / ($allCacheInteractions->hits + $allCacheInteractions->misses) * 100, 2).'%' }}
{{ ((int) ($allCacheInteractions->hits / ($allCacheInteractions->hits + $allCacheInteractions->misses) * 10000)) / 100 }}%
</span>
<span class="text-xs uppercase font-bold text-gray-500 dark:text-gray-400">
Hit Rate
Expand Down Expand Up @@ -105,7 +105,7 @@
@endif
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
{{ round($interaction->hits / ($interaction->hits + $interaction->misses) * 100, 2).'%' }}
{{ ((int) ($interaction->hits / ($interaction->hits + $interaction->misses) * 10000)) / 100 }}%
</x-pulse::td>
</tr>
@endforeach
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Livewire/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,25 @@
(object) ['key' => 'bar', 'hits' => 2, 'misses' => 2],
]));
});

it('does not round numbers up', function () {
for ($i = 0; $i < 20_000; $i++) {
Pulse::record('cache_hit', 'foo')->count()->onlyBuckets();
}
Pulse::record('cache_miss', 'foo')->count()->onlyBuckets();
Pulse::store();

Livewire::test(Cache::class, ['lazy' => false])
->assertDontSeeHtml("100.00%\n")
->assertSeeHtml("99.99%\n");
});

it('does not show decimals for round numbers', function () {
Pulse::record('cache_hit', 'foo')->count()->onlyBuckets();
Pulse::record('cache_miss', 'foo')->count()->onlyBuckets();
Pulse::store();

Livewire::test(Cache::class, ['lazy' => false])
->assertDontSeeHtml("50.00%\n")
->assertSeeHtml("50%\n");
});

0 comments on commit 5f63272

Please sign in to comment.