Simplify human-readable size output
* Limit human-readable sizes to a single decimal * Drop trailing zero * Step one suffix further in the case of a size being 1024 Change-Id: I2eb8ac0571d3d08b52f62155912863870573a37c
This commit is contained in:
@@ -175,8 +175,11 @@ def make_size_human_readable(size):
|
|||||||
base = 1024.0
|
base = 1024.0
|
||||||
|
|
||||||
index = 0
|
index = 0
|
||||||
while size > base:
|
while size >= base:
|
||||||
index = index + 1
|
index = index + 1
|
||||||
size = size / base
|
size = size / base
|
||||||
|
|
||||||
return "%.3f%s" % (size, suffix[index])
|
padded = '%.1f' % size
|
||||||
|
stripped = padded.rstrip('0').rstrip('.')
|
||||||
|
|
||||||
|
return '%s%s' % (stripped, suffix[index])
|
||||||
|
@@ -45,6 +45,8 @@ class TestUtils(unittest.TestCase):
|
|||||||
data = ''.join([f for f in utils.integrity_iter(fixture, checksum)])
|
data = ''.join([f for f in utils.integrity_iter(fixture, checksum)])
|
||||||
|
|
||||||
def test_make_size_human_readable(self):
|
def test_make_size_human_readable(self):
|
||||||
self.assertEqual("1024.000kB", utils.make_size_human_readable(1048576))
|
self.assertEqual("106B", utils.make_size_human_readable(106))
|
||||||
self.assertEqual("1.375GB", utils.make_size_human_readable(1476395008))
|
self.assertEqual("1000kB", utils.make_size_human_readable(1024000))
|
||||||
self.assertEqual("9.309MB", utils.make_size_human_readable(9761280))
|
self.assertEqual("1MB", utils.make_size_human_readable(1048576))
|
||||||
|
self.assertEqual("1.4GB", utils.make_size_human_readable(1476395008))
|
||||||
|
self.assertEqual("9.3MB", utils.make_size_human_readable(9761280))
|
||||||
|
Reference in New Issue
Block a user