2012-09-21 136 views
2

我正在尝试编写一个应用程序来将字节转换为kb到mb到gb到tb。 这是我到目前为止有:Python格式大小的应用程序(将B转换为KB,MB,GB,TB)

def size_format(b): 
    if b < 1000: 
       return '%i' % b + 'B' 
    elif 1000 <= b < 1000000: 
     return '%.1f' % float(b/1000) + 'KB' 
    elif 1000000 <= b < 1000000000: 
     return '%.1f' % float(b/1000000) + 'MB' 
    elif 1000000000 <= b < 1000000000000: 
     return '%.1f' % float(b/1000000000) + 'GB' 
    elif 1000000000000 <= b: 
     return '%.1f' % float(b/1000000000000) + 'TB' 

的问题是,当我尝试应用我得到的小数归零后的一切。 例如 size_format(623)产量 '623B' 但size_format(6200)的 而不是让 '6.2kb' 我得到 '6.0kb'。 任何想法为什么?

+0

用于未来的提示:当你在代码粘贴,选择这一切并使用'{}'按钮,将其格式化为代码。 –

回答

0

做浮动(b)之前做分割,例如做float(b)/1000而不是float(b/1000),因为b和1000都是整数,b/1000仍然是一个没有小数部分的整数。

1

当您使用整数除法分割值时,因为这两个值都是整数。你需要将它们转换的一个第一浮动:

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

甚至只是

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

而不是修改你的代码,你可以改变分裂的行为:

from __future__ import division 

这在Python 2.x使用的“经典”风格上提供了“真正”的划分。有关更多详细信息,请参见PEP 238 - Changing the Division Operator

这是现在在Python 3.x的默认行为

+0

我很习惯其他语言的工作方式,我一直在忘记这一点。当我开始使用Python 3的时候,它真的会咬我。 –

1
def humanbytes(B): 
    'Return the given bytes as a human friendly KB, MB, GB, or TB string' 
    B = float(B) 
    KB = float(1024) 
    MB = float(KB ** 2) # 1,048,576 
    GB = float(KB ** 3) # 1,073,741,824 
    TB = float(KB ** 4) # 1,099,511,627,776 

    if B < KB: 
     return '{0} {1}'.format(B,'Bytes' if 0 == B > 1 else 'Byte') 
    elif KB <= B < MB: 
     return '{0:.2f} KB'.format(B/KB) 
    elif MB <= B < GB: 
     return '{0:.2f} MB'.format(B/MB) 
    elif GB <= B < TB: 
     return '{0:.2f} GB'.format(B/GB) 
    elif TB <= B: 
     return '{0:.2f} TB'.format(B/TB) 

tests = [1, 1024, 500000, 1048576, 50000000, 1073741824, 5000000000, 1099511627776, 5000000000000] 

for t in tests: print '{0} == {1}'.format(t,humanbytes(t)) 

输出:

1 == 1.0 Byte 
1024 == 1.00 KB 
500000 == 488.28 KB 
1048576 == 1.00 MB 
50000000 == 47.68 MB 
1073741824 == 1.00 GB 
5000000000 == 4.66 GB 
1099511627776 == 1.00 TB 
5000000000000 == 4.55 TB 

并为未来的我这里是在Perl太:

sub humanbytes { 
    my $B = shift; 
    my $KB = 1024; 
    my $MB = $KB ** 2; # 1,048,576 
    my $GB = $KB ** 3; # 1,073,741,824 
    my $TB = $KB ** 4; # 1,099,511,627,776 

    if ($B < $KB) { 
     return "$B " . (($B == 0 || $B > 1) ? 'Bytes' : 'Byte'); 
    } elsif ($B >= $KB && $B < $MB) { 
     return sprintf('%0.02f',$B/$KB) . ' KB'; 
    } elsif ($B >= $MB && $B < $GB) { 
     return sprintf('%0.02f',$B/$MB) . ' MB'; 
    } elsif ($B >= $GB && $B < $TB) { 
     return sprintf('%0.02f',$B/$GB) . ' GB'; 
    } elsif ($B >= $TB) { 
     return sprintf('%0.02f',$B/$TB) . ' TB'; 
    } 
} 
5

我具有相当可读的功能,可将字节转换为更大的单位:

def bytes_2_human_readable(number_of_bytes): 
    if number_of_bytes < 0: 
     raise ValueError("!!! number_of_bytes can't be smaller than 0 !!!") 

    step_to_greater_unit = 1024. 

    number_of_bytes = float(number_of_bytes) 
    unit = 'bytes' 

    if (number_of_bytes/step_to_greater_unit) >= 1: 
     number_of_bytes /= step_to_greater_unit 
     unit = 'KB' 

    if (number_of_bytes/step_to_greater_unit) >= 1: 
     number_of_bytes /= step_to_greater_unit 
     unit = 'MB' 

    if (number_of_bytes/step_to_greater_unit) >= 1: 
     number_of_bytes /= step_to_greater_unit 
     unit = 'GB' 

    if (number_of_bytes/step_to_greater_unit) >= 1: 
     number_of_bytes /= step_to_greater_unit 
     unit = 'TB' 

    precision = 1 
    number_of_bytes = round(number_of_bytes, precision) 

    return str(number_of_bytes) + ' ' + unit 
0

这里是将字节转换为千,兆,万亿。

#From bytes to kilo, mega, tera 
def get_(size): 

    #2**10 = 1024 
    power = 2**10 
    n = 1 
    Dic_powerN = {1:'kilobytes', 2:'megabytes', 3:'gigabytes', 4:'Terabytes'} 

    if size <= power**2 : 
     size /= power 
     return size, Dic_powerN[n] 

    else: 
     while size > power : 
      n += 1 
      size /= power**n 

     return size, Dic_powerN[n] 
相关问题