Python format size application (converting B to KB, MB, GB, TB)

前端 未结 15 1972
轮回少年
轮回少年 2021-02-01 16:13

I am trying to write an application to convert bytes to kb to mb to gb to tb. Here\'s what I have so far:

def size_format(b):
    if b < 1000:
              r         


        
15条回答
  •  忘了有多久
    2021-02-01 16:32

    When you divide the value you're using an integer divide, since both values are integers. You need to convert one of them to float first:

    return '%.1f' % float(b)/1000 + 'KB'
    

    or even just

    return '%.1f' % b/1000.0 + 'KB'
    

提交回复
热议问题