Reputation: 1
I am trying to create a readable file name generator given the number of bytes, but also doing it without if statements or loops:
def memory_size(n):
suffix = ['B','KB','MB','GB','TB']
return ('%.1f'+suffix[int(math.log10(n)/3)]) %(n/?)
I am stumped as to what i can divide 'n' by so that it will show the user the decreased file size without using an if statement or a loop?
Upvotes: 0
Views: 507
Reputation:
1024**(math.log10(n)/3)
The stupid thing about not using for loops is you're gonna get annoying float division errors
>>> mem(1000000)
'1.0MB'
>>> mem(1000000000)
'0.9GB'
I'd put a call to round()
in your code
Upvotes: 1