def metric_format(num, unit='s', digits=1, align_unit=False):
    """Format and scale output with metric prefixes.

    Example
    -------

    >>> metric_format(0)
    '0.0 s'
    >>> metric_format(0, align_unit=True)
    '0.0 s '
    >>> metric_format(0.002, unit='B')
    '2.0 mB'
    >>> metric_format(2001, unit='B')
    '2.0 KB'
    >>> metric_format(2001, unit='B', digits=3)
    '2.001 KB'

    """
    if num is None:
        return '<unknown>'
    prefix = ''
    if num >= 1.0 or num == 0.0:
        if num >= 1000.0:
            num /= 1000.0
            for p in ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Yi']:
                prefix = p
                if abs(num) < 1000.0:
                    break
                num /= 1000.0
    else:
        num *= 1000.0
        for p in ['m', 'u', 'n', 'p', 'f']:
            prefix = p
            if abs(round(num, digits + 3)) >= 1:
                break
            num *= 1000.0
    if prefix == '' and align_unit:
        return ('%.' + str(digits) + 'f %s ') % (num, unit)
    else:
        return ('%.' + str(digits) + 'f %s%s') % (num, prefix, unit)
