this question has answer here:
- better way convert file sizes in python 10 answers
what fastest way covert values given in mb , kb gb , tb?
sizes = ['999.992 mb', '2.488 gb', '401 kb']
sizes_in_gb = ['?', '?', '?']
sizes_in_tb = ['?', '?', '?']
given:
>>> sizes = ['999.992 mb', '2.488 gb', '401 kb']
first agree on 'precision' means. since input float, fair assumption 'precision' limited input precision.
to calculate, first convert base bytes (know though actual precision no better input precision):
>>> defs={'kb':1024, 'mb':1024**2, 'gb':1024**3, 'tb':1024**4} >>> bytes=[float(lh)*defs[rh] lh, rh in [e.split() e in sizes]] >>> bytes [1048567611.392, 2671469658.112, 410624.0]
then convert magnitude desired:
>>> sd='gb' >>> ['{:0.2} {}'.format(e/defs[sd], sd) e in bytes] ['0.98 gb', '2.5 gb', '0.00038 gb'] >>> sd='mb' >>> ['{:0.2} {}'.format(e/defs[sd], sd) e in bytes] ['1e+03 mb', '2.5e+03 mb', '0.39 mb'] >>> sd='tb' >>> ['{:0.2} {}'.format(e/defs[sd], sd) e in bytes] ['0.00095 tb', '0.0024 tb', '3.7e-07 tb']
Comments
Post a Comment