From 94191fa144684ecfe8b3e036f7fcd7173aceb341 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 15 Nov 2019 21:34:10 +0000 Subject: [PATCH] Convert : if the value is 0 for humansize, return 0.000 git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5731 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/convertTest.php | 7 +++++++ convert.php | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Tests/convertTest.php b/Tests/convertTest.php index b4bd8c9..ed276bc 100644 --- a/Tests/convertTest.php +++ b/Tests/convertTest.php @@ -105,6 +105,13 @@ class test_convert extends PHPUnit_Framework_TestCase $this->assertSame ($res, "-12.35kB"); } + public function test_humanSize_8 () + { + $res = \convert::humanSize (0, 2); + $this->assertSame ($res, "0.00B"); + } + + public function test_humanSize_error1 () { $this->expectException ("Exception", diff --git a/convert.php b/convert.php index 29f78a3..31751c0 100644 --- a/convert.php +++ b/convert.php @@ -131,12 +131,17 @@ class convert 6 => 'E', // exa 7 => 'Z', // zetta 8 => 'Y');// yotta - for ($factor = 8 ; $factor >= -8 ; $factor--) + for ($factor = 8 ; $factor > -8 ; $factor--) { if (abs ($value) >= pow ($power, $factor)) break; } - if ($factor > 0) + if ($value == 0) + { + $display = 0; + $factor = 0; + } + elseif ($factor > 0) $display = $value / pow ($power, $factor); elseif ($factor < 0) $display = $value * pow ($power, -1 * $factor);