简体   繁体   English

如何减小float-python的大小

[英]How to decrease the size of a float-python

I am trying to create a readable file name generator given the number of bytes, but also doing it without if statements or loops: 我正在尝试根据给定的字节数创建一个可读的文件名生成器,但也要在没有if语句或循环的情况下这样做:

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? 我很困惑我可以将“ n”除以什么,这样它将向用户显示减小的文件大小,而无需使用if语句或循环?

1024**(math.log10(n)/3)

The stupid thing about not using for loops is you're gonna get annoying float division errors 关于不使用for循环的愚蠢的事情是,您将得到烦人的float除法错误

>>> mem(1000000)
'1.0MB'
>>> mem(1000000000)
'0.9GB'

I'd put a call to round() in your code 我会在您的代码中调用round()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM