When stored in a database, the use of Bytes is more accurate, scalable, and highly flexible.
When you output, you need to do some matching.
1. Precautions and Test code
1. You need to consider the scenario where sizeInBytes is None.
2. Divide by 1024.0 rather than 1024 to avoid loss of precision.
The implemented function is GETSIZEINMB (sizeinbytes), and the generic test code is
2. Output in MB--return float
In general, the size of the ebook between 1-50MB, the output of a unified conversion to MB is a good choice.
Disadvantages:
1. Output accuracy is too high, such as 10240000 Bytes calculation results of 10240000-9.765625
2. File size is limited, less than 1 MB or G-level data is not suitable for this way display
Advantage:
1. Suitable for participating in the calculation with return value
3. Keep 1 decimal places in megabytes--return str
With precision in mind, you can choose to retain 1 decimal places.
def GETSIZEINMB (sizeinbytes):
Return '%.1f '% ((sizeinbytes or 0)/(1024.0*1024.0),) # Use 1-dimension tuple is suggested
The return value is suggested as '%.1f '% (number,) rather than '%.1f '%
Both can be executed correctly, but the latter is easily misjudged as a function that executes only one parameter number, leading to difficult-to-judge errors.
3. Keep up to 1 decimal places in megabytes--return str
Most operating systems typically display up to 1 decimal places
4. Automatically select the best unit
Algorithm Description:
1. From the English grammar point of view, only 1 uses the singular form. The other 0/decimals are in plural form. Bytes level involved
2. In terms of accuracy, KB and above, retain 1 decimal places. Bytes reserves up to 1 decimal places.
This processing rule is not suitable for cases where decimals are very bit 0, such as 10.0 bytes,10.01 bytes. The input results are all bytes.
In other cases, there is no problem with precision.
Test data and results such as
The above content to introduce to you based on Python implementation of file size output of the relevant knowledge, I hope this article to share to help.