43 lines
959 B
PHP
43 lines
959 B
PHP
<?php
|
|
|
|
namespace Helper;
|
|
|
|
/** All raw mathematics in one place. */
|
|
class Math
|
|
{
|
|
/**
|
|
* @param float|int $total
|
|
* @param int|null $number
|
|
* @return float
|
|
*/
|
|
public static function getPercentage($total, ?int $number): float
|
|
{
|
|
if (is_null($number)) {
|
|
return 0;
|
|
}
|
|
return $total > 0 ? round(($number * 100) / $total, 2) : 0;
|
|
}
|
|
|
|
|
|
public static function get100Percentage($total, ?int $number)
|
|
{
|
|
if (is_null($number)) {
|
|
return 0;
|
|
}
|
|
return min(self::getPercentage($total, $number), 100);
|
|
}
|
|
|
|
/** Number-20% and Number+20% */
|
|
public static function get20PercentRange($number): array
|
|
{
|
|
return [
|
|
'min' => $number * ((100 - 20) / 100),
|
|
'max' => $number * ((100 + 20) / 100),
|
|
];
|
|
}
|
|
|
|
public static function addPercent($num, $percent)
|
|
{
|
|
return $num + (($percent / 100) * $num);
|
|
}
|
|
} |