0

Below is the code:

if number_of_bytes < 0:
    raise ValueError("!!! numberOfBytes can't be smaller than 0 !!!")
step_to_greater_unit = 1024.
number_of_bytes = float(number_of_bytes)
unit = 'bytes'
if (number_of_bytes / step_to_greater_unit) >= 1:
    number_of_bytes /= step_to_greater_unit
    unit = 'KB'
if (number_of_bytes / step_to_greater_unit) >= 1:
    number_of_bytes /= step_to_greater_unit
    unit = 'MB'
if (number_of_bytes / step_to_greater_unit) >= 1:
    number_of_bytes /= step_to_greater_unit
    unit = 'GB'
if (number_of_bytes / step_to_greater_unit) >= 1:
    number_of_bytes /= step_to_greater_unit
    unit = 'TB'
precision = 3
number_of_bytes = round(number_of_bytes, precision)
print number_of_bytes
size = str(number_of_bytes) + '' + unit

Below is the sample output:

 1.125TB


 1.406TB

Expected output:

 1.12TB


 1.41TB

when my precision value is 2 I am getting 1.13TB but I need to round off to 1.12TB .

Basically 1.125TB I need two decimal points. 1.125 is rounding off to 1.13 but it should round of to 1.12 and 1.406TB should round off to 1.41TB

Could you please help how to get my expected result

Bittu
  • 31
  • 2
  • 9
  • 1
    Maybe you are looking for [floor](https://docs.python.org/2/library/math.html)? – user3764893 Jun 23 '17 at 11:03
  • so your expected output will be 1.12 TB and 1.40TB right? – Stanly Jun 23 '17 at 11:06
  • 1
    Check this out https://stackoverflow.com/questions/783897/truncating-floats-in-python – Stanly Jun 23 '17 at 11:08
  • 1.125 rounds to 1.13, though. If you want to floor it, you should use the floor() function, instead of round(). – Zinki Jun 23 '17 at 11:14
  • You can do something like `number_of_bytes = math.floor(number_of_bytes*100)/100.0` – user3764893 Jun 23 '17 at 11:16
  • Hi all just added expected results in question. math floor is worked for 1.12 but for 1.406 it is printing 1.4TB but it should be 1.41TB – Bittu Jun 23 '17 at 13:22
  • Possible duplicate of [Truncating floats in Python](https://stackoverflow.com/questions/783897/truncating-floats-in-python) – bereal Jun 23 '17 at 13:23
  • Why do you want to round `1.125TB` off to `1.12TB`? That's not how rounding works. `1.13TB` is correct. _You_ are being inconsistent, that's why `round` works for one, and `floor` for the other of your numbers. – tobias_k Jun 23 '17 at 13:33
  • @tobias_k One of my device is showing 1.12TB for 1152GB input. So i need to validate device size. I need to write script as per device output. I know rounding doing correct but this is my situation – Bittu Jun 23 '17 at 13:38
  • Well, maybe those "1152 GB" are already rounded, too? Suppose the _actual_ size of that device is 1151.7 GB, then that would be ~1.1247 TB, which is correctly rounded down to 1.12 TB. – tobias_k Jun 23 '17 at 13:44
  • No, I guess because we provided input as 1152 only or it may doing different calucalations – Bittu Jun 23 '17 at 13:51
  • Yes, you are providing the input as 1152, but that input is already rounded, so the rounding error propagates. But that's not something your program should be concerned with. Just provide the correct output for the given input, which is 1.25 and thus 1.3. – tobias_k Jun 23 '17 at 14:04

1 Answers1

0

Would suggest you to look into the hurry package, which eases things a bit when it comes to file size prints.

>>> from hurry.filesize import alternative
>>> size(1, system=alternative)
'1 byte'
>>> size(10, system=alternative)
'10 bytes'
>>> size(1024, system=alternative)
'1 KB'

https://pypi.python.org/pypi/hurry.filesize

Roelant
  • 4,508
  • 1
  • 32
  • 62