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