Skip to content

Commit bc08704

Browse files
authored
[Bug] Divide by zero (alexjustesen#1340)
1 parent 12ff490 commit bc08704

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

app/Helpers/Number.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static function bitsToHuman(int|float $bits, int $precision = 0, ?int $ma
3535
{
3636
$units = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
3737

38+
if ($bits === 0) {
39+
return '0 B';
40+
}
41+
3842
for ($i = 0; ($bits / 1000) > 0.99 && ($i < count($units) - 1); $i++) {
3943
$bits /= 1000;
4044
}
@@ -51,6 +55,10 @@ public static function toBitRate(int|float $bits, int $precision = 0, ?int $maxP
5155
{
5256
$units = ['Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];
5357

58+
if ($bits === 0) {
59+
return '0 B';
60+
}
61+
5462
for ($i = 0; ($bits / 1000) > 0.99 && ($i < count($units) - 1); $i++) {
5563
$bits /= 1000;
5664
}

app/helpers.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function toBits(float $size, int $precision = 4): float
2727
if (! function_exists('percentChange')) {
2828
function percentChange(float $dividend, float $divisor, int $precision = 0): string
2929
{
30+
if ($dividend === 0 || $divisor === 0) {
31+
return 0;
32+
}
33+
3034
$quotient = ($dividend - $divisor) / $divisor;
3135

3236
return number_format(round($quotient * 100, $precision), $precision);

0 commit comments

Comments
 (0)