2018-05-31 22:01:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Util\Lexer;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
class PrettyNumber
|
|
|
|
{
|
2021-06-07 23:18:06 +00:00
|
|
|
public static function convert($number, $showDecimals = true)
|
|
|
|
{
|
|
|
|
if(!is_integer($number)) {
|
|
|
|
return $number;
|
|
|
|
}
|
2020-11-18 21:19:02 +00:00
|
|
|
|
2021-06-07 23:18:06 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 22:01:31 +00:00
|
|
|
|
2021-06-07 23:18:06 +00:00
|
|
|
return $number;
|
|
|
|
}
|
2018-06-02 22:53:35 +00:00
|
|
|
|
2021-06-07 23:18:06 +00:00
|
|
|
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];
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2021-06-07 23:18:06 +00:00
|
|
|
return $res;
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
}
|