1

What would be the fastest way to covert the values given in MB and KB to GB and TB?

sizes = ['999.992 MB', '2.488 GB', '401 KB']

sizes_in_GB = ['?', '?', '?']

sizes_in_TB = ['?', '?', '?']

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • Given your example data, there are at least two phases: (1) Parse these strings (2) Do the calculation. What's your question exactly? Define precision! The simple approach is just a (vectorized = numpy) multiplication. The less simple/performant approach will use [decimal](https://docs.python.org/2/library/decimal.html) (for maximum precision) – sascha Sep 02 '16 at 04:01
  • numpy solution would work well. Please illustrate! – alphanumeric Sep 02 '16 at 04:04
  • Giving a numpy example is kind of wasted work without more information about your data (which decides which approach is good and which is not). If your sizes array is not big, there is no need to use numpy. – sascha Sep 02 '16 at 04:05

2 Answers2

8

Given:

>>> sizes = ['999.992 MB', '2.488 GB', '401 KB']

First agree on what 'precision' means. Since your input is a float, it is a fair assumption that 'precision' is limited to the input precision.

To calculate, first convert to base bytes (know though that your actual precision is no better than the input precision):

>>> defs={'KB':1024, 'MB':1024**2, 'GB':1024**3, 'TB':1024**4} 
>>> bytes=[float(lh)*defs[rh] for lh, rh in [e.split() for e in sizes]]
>>> bytes
[1048567611.392, 2671469658.112, 410624.0]

Then convert to magnitude desired:

>>> sd='GB'
>>> ['{:0.2} {}'.format(e/defs[sd], sd) for e in bytes]
['0.98 GB', '2.5 GB', '0.00038 GB']
>>> sd='MB'
>>> ['{:0.2} {}'.format(e/defs[sd], sd) for e in bytes]
['1e+03 MB', '2.5e+03 MB', '0.39 MB']
>>> sd='TB'
>>> ['{:0.2} {}'.format(e/defs[sd], sd) for e in bytes]
['0.00095 TB', '0.0024 TB', '3.7e-07 TB']
jackotonye
  • 3,537
  • 23
  • 31
dawg
  • 98,345
  • 23
  • 131
  • 206
1

Heres something I found online.

def conv_KB_to_MB(input_kilobyte):
        megabyte = 1./1000
        convert_mb = megabyte * input_kilobyte
        return convert_mb
def conv_MB_to_GB(input_megabyte):
        gigabyte = 1.0/1024
        convert_gb = gigabyte * input_megabyte
        return convert_gb
#Create the menu


print "Enter 1 to convert from KBs to MBs"
print "Enter 2 to convert from MBs to GBs"


try:
        menu_choice = (raw_input("Enter a selection"))
except ValueError:
        print "This is not a number"
except NameError:
        print "Name Error"
except SystenError:
        print "Syntax Error"

if menu_choice == '1':
        kb_input = float(input("Enter KBs"))
        megabytes = conv_KB_to_MB(kb_input)
        print megabytes

elif menu_choice == '2':
        mb_input = float(input("Enter MBs"))
        gigabytes = conv_MB_to_GB(mb_input)
        print gigabytes
else:
        print "exiting"

Source:http://www.sfentona.net/?p=1965

thesonyman101
  • 791
  • 7
  • 16
  • 2
    This is **super ugly**. I would **always** prefer ```1.0/1024``` to ```float(0.000976562)``` if i would use fp-arithmetic at all. – sascha Sep 02 '16 at 04:08
  • But a gigabyte is 1024 megabytes, so there should not be a number like 9e-7 in the MB to GB conversion. The constants should be the same, right? – EL_DON Sep 02 '16 at 04:10
  • 1
    @EL_DON At least the two numbers should be the same – sascha Sep 02 '16 at 04:11
  • fixed the gb conversion Idk the math for mbs – thesonyman101 Sep 02 '16 at 04:11
  • 2
    @sascha And they should be 1./1024. – EL_DON Sep 02 '16 at 04:12
  • I'm only in geometry so don't hate plz XD – thesonyman101 Sep 02 '16 at 04:12
  • @EL_DON I wish people would *stop* using multiples of 1024! It's acceptable if you're using it as an approximation for RAM size, which *only* comes in powers of 2, but for everything else the traditional prefix meanings of powers of 1000 should be retained. – Mark Ransom Sep 02 '16 at 12:50
  • @MarkRansom I will accept that in this context, MB and GB were used instead of MiB and GiB, so 1000 would be appropriate here specifically. I don't agree that 1 KiB = 1024 Bytes is bad in general. I will be careful to use GiB/MiB/KiB when using the 1024 definitions in the future. – EL_DON Sep 02 '16 at 17:43