Update PrettyNumber, fix deprecated warning

This commit is contained in:
Daniel Supernault 2021-06-07 17:18:06 -06:00
parent 91cc9adf52
commit 20ec870bf9
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -4,40 +4,41 @@ namespace App\Util\Lexer;
class PrettyNumber
{
public static function convert($number, $showDecimals = true)
{
if(!is_integer($number)) {
return $number;
}
public static function convert($number, $showDecimals = true)
{
if(!is_integer($number)) {
return $number;
}
$abbrevs = [12 => 'T', 9 => 'B', 6 => 'M', 3 => 'K', 0 => ''];
foreach ($abbrevs as $exponent => $abbrev) {
if(abs($number) >= pow(10, $exponent)) {
$display = $number / pow(10, $exponent);
$decimals = !$showDecimals ? 0 : ($exponent >= 3 && round($display) < 100) ? 1 : 0;
$number = number_format($display, $decimals).$abbrev;
break;
}
}
$abbrevs = [12 => 'T', 9 => 'B', 6 => 'M', 3 => 'K', 0 => ''];
foreach ($abbrevs as $exponent => $abbrev) {
if(abs($number) >= pow(10, $exponent)) {
$display = $number / pow(10, $exponent);
$decimals = ($exponent >= 3 && round($display) < 100) ? 1 : 0;
$decimals = !$showDecimals ? 0 : $decimals;
$number = number_format($display, $decimals).$abbrev;
break;
}
}
return $number;
}
return $number;
}
public static function size($expression, $kb = false)
{
if ($kb) {
$expression = $expression * 1024;
}
$size = intval($expression);
$precision = 0;
$short = true;
$units = $short ?
['B', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] :
['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
}
$res = round($size, $precision).$units[$i];
public static function size($expression, $kb = false)
{
if ($kb) {
$expression = $expression * 1024;
}
$size = intval($expression);
$precision = 0;
$short = true;
$units = $short ?
['B', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] :
['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
}
$res = round($size, $precision).$units[$i];
return $res;
}
return $res;
}
}