Convert : update humanSize to supports negative numbers or numbers between -1 and 1
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5728 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
70
convert.php
70
convert.php
@@ -84,28 +84,58 @@ class convert
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Convert the provided integer to human readable format
|
||||
* Example : 1440000000 => 1.44Mo
|
||||
* @param integer $bytes The number of bytes to convert
|
||||
* @param integer $decimals The number of decimal
|
||||
/** Convert the provided float to human readable format
|
||||
* Example : 1440000 => 1.44MB
|
||||
* @param float|integer $value The number to convert
|
||||
* @param integer|null $decimals The number of decimal (2 by default)
|
||||
* @param integer|null $power (1000 by default or 1024)
|
||||
* @param string|null $unit The Unit displayed after the multiplier (B for
|
||||
* Bytes by default)
|
||||
*/
|
||||
public static function humanSize ($bytes, $decimals = 2)
|
||||
public static function humanSize ($value, $decimals = 2, $power = 1000,
|
||||
$unit = "B")
|
||||
{
|
||||
$size = array (dgettext ("domframework", 'B'),
|
||||
dgettext ("domframework", 'kB'),
|
||||
dgettext ("domframework", 'MB'),
|
||||
dgettext ("domframework", 'GB'),
|
||||
dgettext ("domframework", 'TB'),
|
||||
dgettext ("domframework", 'PB'),
|
||||
dgettext ("domframework", 'EB'),
|
||||
dgettext ("domframework", 'ZB'),
|
||||
dgettext ("domframework", 'YB'));
|
||||
$factor = floor ((strlen ($bytes) - 1) / 3);
|
||||
if ($factor == 0)
|
||||
$val = sprintf ("%d", intval ($bytes)). @$size[$factor];
|
||||
if (! is_integer ($value) && ! is_float ($value))
|
||||
throw new \Exception ("convert::humanSize value not numerical : ".
|
||||
gettype ($value), 500);
|
||||
if (! is_integer ($decimals))
|
||||
throw new \Exception ("convert::humanSize decimal not integer : ".
|
||||
gettype ($decimals), 500);
|
||||
if ($decimals < 0)
|
||||
throw new \Exception ("convert::humanSize decimal value negative", 500);
|
||||
if ($power !== 1000 && $power !== 1024)
|
||||
throw new \Exception ("convert::humanSize power value !== 1000 and 1024".
|
||||
" : ".var_export ($power, true), 500);
|
||||
$size = array (
|
||||
-8 => 'y', // yocto
|
||||
-7 => 'z', // zepto
|
||||
-6 => 'a', // atto
|
||||
-5 => 'f', // femto
|
||||
-4 => 'p', // pico
|
||||
-3 => 'n', // nano
|
||||
-2 => 'u', // micro
|
||||
-1 => 'm', // milli
|
||||
0 => '',
|
||||
1 => 'k', // kilo
|
||||
2 => 'M', // mega
|
||||
3 => 'G', // giga
|
||||
4 => 'T', // tera
|
||||
5 => 'P', // peta
|
||||
6 => 'E', // exa
|
||||
7 => 'Z', // zetta
|
||||
8 => 'Y');// yotta
|
||||
for ($factor = 8 ; $factor >= -8 ; $factor--)
|
||||
{
|
||||
if (abs ($value) >= pow ($power, $factor))
|
||||
break;
|
||||
}
|
||||
if ($factor > 0)
|
||||
$display = $value / pow ($power, $factor);
|
||||
elseif ($factor < 0)
|
||||
$display = $value * pow ($power, -1 * $factor);
|
||||
else
|
||||
$val = sprintf ("%.{$decimals}f", $bytes / pow (1024, $factor)) .
|
||||
@$size[$factor];
|
||||
return $val;
|
||||
$display = $value;
|
||||
|
||||
return sprintf ("%.${decimals}f", $display).$size[$factor].$unit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user