def format_filesize( bytesize, method = None):
    """
    Formats the given size in bytes to be human-readable, 
    either the most appropriate form (B, KB, MB, GB) or 
    a form specified as optional second parameter (e.g. "MB").

    Returns a localized "(unknown)" string when the bytesize
    has a negative value.
    """
    methods = {
        'GB': 1024.0 * 1024.0 * 1024.0,
        'MB': 1024.0 * 1024.0,
        'KB': 1024.0,
        'B':  1.0
    }
    
    methods_decimal = {
    	'GB': 1000.0 * 1000.0 * 1000.0,
	'MB': 1000.0 * 1000.0,
	'KB': 1000.0,
	'B':  1.0
    }
    
    methods_binary = {
        'GiB': 1024.0 * 1024.0 * 1024.0,
        'MiB': 1024.0 * 1024.0,
        'KiB': 1024.0,
        'B':   1.0
    }
    
    equivalent = {
        'GB': 'GiB',
	'MB': 'MiB',
	'KB': 'KiB'
    }

    try:
	    byteprefix = os.environ['BYTEPREFIX']
    except:
	    byteprefix = 'off'
    
    try:
        bytesize = float( bytesize)
    except:
        return _('(unknown)')

    if bytesize < 0:
        return _('(unknown)')

    if byteprefix == 'off':
        if method not in methods:
	    method = 'B'
            for trying in ('KB', 'MB', 'GB'):
                if bytesize >= methods[trying]:
                    method = trying
        return '%.2f %s' % ( bytesize / methods[method], method,)

    if byteprefix == 'binary':
        for bptrying in ('KB', 'MB', 'GB'):
            if method == bptrying:
                method = equivalent[bptrying]
        if method not in methods_binary:
            method = 'B'
            for trying in ('KiB', 'MiB', 'GiB'):
                if bytesize >= methods_binary[trying]:
                    method = trying
        return '%.2f %s' % (bytesize / methods_binary[method], method,)
				
    if byteprefix == 'decimal':
        if method not in methods_decimal:
	    method = 'B'
            for trying in ('KB', 'MB', 'GB'):
                if bytesize >= methods_decimal[trying]:
                    method = trying
        return '%.2f %s' % (bytesize / methods_decimal[method], method,)