This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_bytes(num, suffix="B"): | |
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"): | |
if abs(num) < 1024.0: | |
return f"{num:3.1f}{unit}{suffix}" | |
num /= 1024.0 | |
return f"{num:.1f}Yi{suffix}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_bytes(size, decimal_places=2): | |
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']: | |
if size < 1024.0 or unit == 'PiB': | |
break | |
size /= 1024.0 | |
return f"{size:.{decimal_places}f} {unit}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import log | |
unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 1, 2, 2, 2]) | |
def format_bytes(num): | |
if num > 1: | |
exponent = min(int(log(num, 1024)), len(unit_list) - 1) | |
quotient = float(num) / 1024**exponent | |
unit, num_decimals = unit_list[exponent] | |
format_string = '{:.%sf} {}' % (num_decimals) | |
return format_string.format(quotient, unit) | |
if num == 0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import log | |
def format_bytes(n,pow=0,b=1024,u='B',pre=['']+[p+'i'for p in'KMGTPEZY']): | |
pow,n=min(int(log(max(n*b**pow,1),b)),len(pre)-1),n*b**pow | |
return "%%.%if %%s%%s"%abs(pow%(-pow-1))%(n/b**float(pow),pre[pow],u) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_bytes(bytes, units=[' bytes','KB','MB','GB','TB', 'PB', 'EB']): | |
return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:]) if units[1:] else f'{bytes>>10}ZB' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_bytes(num, suffix='B'): | |
magnitude = int(math.floor(math.log(num, 1024))) | |
val = num / math.pow(1024, magnitude) | |
if magnitude > 7: | |
return '{:.1f}{}{}'.format(val, 'Yi', suffix) | |
return '{:3.1f}{}{}'.format(val, ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi'][magnitude], suffix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_bytes(n): # Convert a number of bytes into a human readable format | |
symbols = ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB') | |
prefix = {} | |
for i, s in enumerate(symbols): | |
prefix[s] = 1 << (i + 1) * 10 | |
for s in reversed(symbols): | |
if n >= prefix[s]: | |
value = float(n) / prefix[s] | |
return '%.3f%s' % (value, s) # 3 digits | |
return "%sB" % n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// convert bytes format | |
function format_Bytes($size,$level=0,$precision=2,$base=1024) | |
{ | |
if ($size === '0' || $size === null) { | |
return "0 B"; | |
} | |
else | |
$unit = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB','YB'); | |
$times = floor(log($size,$base)); | |
return sprintf("%.".$precision."f",$size/pow($base,($times+$level)))." ".$unit[$times+$level]; |