37 lines
831 B
PHP
37 lines
831 B
PHP
<?php
|
|
|
|
namespace Insallah;
|
|
|
|
/** 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
|
|
{
|
|
return $total > 0 ? round(($number * 100) / $total, 2) : 0;
|
|
}
|
|
|
|
|
|
public static function get100Percentage($total, int $number)
|
|
{
|
|
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);
|
|
}
|
|
} |