game/_incl_data/class/Helper/Math.php

43 lines
959 B
PHP
Raw Normal View History

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