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:
@@ -56,4 +56,80 @@ class test_convert extends PHPUnit_Framework_TestCase
|
|||||||
$res = \convert::ucwords ("édou-ard d'étienne", " -'");
|
$res = \convert::ucwords ("édou-ard d'étienne", " -'");
|
||||||
$this->assertSame ($res, "Édou-Ard D'Étienne");
|
$this->assertSame ($res, "Édou-Ard D'Étienne");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
// humanSize //
|
||||||
|
/////////////////////
|
||||||
|
public function test_humanSize_1 ()
|
||||||
|
{
|
||||||
|
$res = "";
|
||||||
|
for ($i = -8 ; $i <= 8 ; $i++)
|
||||||
|
{
|
||||||
|
$res .= \convert::humanSize (1.441234 * pow (1000, $i), 2, 1000)."\n";
|
||||||
|
}
|
||||||
|
$this->assertSame ($res, "1.44yB\n1.44zB\n1.44aB\n1.44fB\n1.44pB\n1.44nB\n".
|
||||||
|
"1.44uB\n1.44mB\n1.44B\n1.44kB\n1.44MB\n1.44GB\n1.44TB\n1.44PB\n1.44EB\n".
|
||||||
|
"1.44ZB\n1.44YB\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_2 ()
|
||||||
|
{
|
||||||
|
$res = \convert::humanSize (1441234);
|
||||||
|
$this->assertSame ($res, "1.44MB");
|
||||||
|
}
|
||||||
|
public function test_humanSize_3 ()
|
||||||
|
{
|
||||||
|
$res = \convert::humanSize (10441234);
|
||||||
|
$this->assertSame ($res, "10.44MB");
|
||||||
|
}
|
||||||
|
public function test_humanSize_4 ()
|
||||||
|
{
|
||||||
|
$res = \convert::humanSize (0.123, 0);
|
||||||
|
$this->assertSame ($res, "123mB");
|
||||||
|
}
|
||||||
|
public function test_humanSize_5 ()
|
||||||
|
{
|
||||||
|
$res = \convert::humanSize (0.12345, 2);
|
||||||
|
$this->assertSame ($res, "123.45mB");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_6 ()
|
||||||
|
{
|
||||||
|
$res = \convert::humanSize (-0.12345, 2);
|
||||||
|
$this->assertSame ($res, "-123.45mB");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_7 ()
|
||||||
|
{
|
||||||
|
$res = \convert::humanSize (-12345, 2);
|
||||||
|
$this->assertSame ($res, "-12.35kB");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_error1 ()
|
||||||
|
{
|
||||||
|
$this->expectException ("Exception",
|
||||||
|
"convert::humanSize value not numerical : string", 500);
|
||||||
|
$res = \convert::humanSize ("1441234");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_error2 ()
|
||||||
|
{
|
||||||
|
$this->expectException ("Exception",
|
||||||
|
"convert::humanSize decimal not integer : double", 500);
|
||||||
|
$res = \convert::humanSize (1441234, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_error3 ()
|
||||||
|
{
|
||||||
|
$this->expectException ("Exception",
|
||||||
|
"convert::humanSize decimal value negative", 500);
|
||||||
|
$res = \convert::humanSize (1441234, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_humanSize_error4 ()
|
||||||
|
{
|
||||||
|
$this->expectException ("Exception",
|
||||||
|
"convert::humanSize power value !== 1000 and 1024 : 2000", 500);
|
||||||
|
$res = \convert::humanSize (1441234, 2, 2000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
70
convert.php
70
convert.php
@@ -84,28 +84,58 @@ class convert
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert the provided integer to human readable format
|
/** Convert the provided float to human readable format
|
||||||
* Example : 1440000000 => 1.44Mo
|
* Example : 1440000 => 1.44MB
|
||||||
* @param integer $bytes The number of bytes to convert
|
* @param float|integer $value The number to convert
|
||||||
* @param integer $decimals The number of decimal
|
* @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'),
|
if (! is_integer ($value) && ! is_float ($value))
|
||||||
dgettext ("domframework", 'kB'),
|
throw new \Exception ("convert::humanSize value not numerical : ".
|
||||||
dgettext ("domframework", 'MB'),
|
gettype ($value), 500);
|
||||||
dgettext ("domframework", 'GB'),
|
if (! is_integer ($decimals))
|
||||||
dgettext ("domframework", 'TB'),
|
throw new \Exception ("convert::humanSize decimal not integer : ".
|
||||||
dgettext ("domframework", 'PB'),
|
gettype ($decimals), 500);
|
||||||
dgettext ("domframework", 'EB'),
|
if ($decimals < 0)
|
||||||
dgettext ("domframework", 'ZB'),
|
throw new \Exception ("convert::humanSize decimal value negative", 500);
|
||||||
dgettext ("domframework", 'YB'));
|
if ($power !== 1000 && $power !== 1024)
|
||||||
$factor = floor ((strlen ($bytes) - 1) / 3);
|
throw new \Exception ("convert::humanSize power value !== 1000 and 1024".
|
||||||
if ($factor == 0)
|
" : ".var_export ($power, true), 500);
|
||||||
$val = sprintf ("%d", intval ($bytes)). @$size[$factor];
|
$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
|
else
|
||||||
$val = sprintf ("%.{$decimals}f", $bytes / pow (1024, $factor)) .
|
$display = $value;
|
||||||
@$size[$factor];
|
|
||||||
return $val;
|
return sprintf ("%.${decimals}f", $display).$size[$factor].$unit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user