phpvms/app/Support/Math.php

54 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Support;
/**
* Helper math
*/
class Math
{
/**
* Determine from the base rate, if we want to return the overridden rate
* or if the overridden rate is a percentage, then return that amount
2018-08-27 00:40:04 +08:00
*
* @param $base_rate
* @param $override_rate
2018-08-27 00:40:04 +08:00
*
* @return float|null
*/
public static function applyAmountOrPercent($base_rate, $override_rate = null): ?float
{
if (!$override_rate) {
return $base_rate;
}
2018-08-27 00:40:04 +08:00
// Not a percentage override
if (substr_count($override_rate, '%') === 0) {
return $override_rate;
}
return static::addPercent($base_rate, $override_rate);
}
/**
* Add/subtract a percentage to a number
2018-08-27 00:40:04 +08:00
*
* @param $number
* @param $percent
2018-08-27 00:40:04 +08:00
*
* @return float
*/
public static function addPercent($number, $percent): float
{
if (!is_numeric($number)) {
$number = (float) $number;
}
if (!is_numeric($percent)) {
$percent = (float) $percent;
}
return $number + ($number * ($percent / 100));
}
}