def sign(x, value=1):
    """Mathematical signum function.
    :param x: Object of investigation
    :param value: The size of the signum (defaults to 1)
    :returns: Plus or minus value
    """
return -value if x < 0 else value

def prefix(x, dimension=1):
    """Give the number an appropriate SI prefix.
    :param x: Too big or too small number.
    :returns: String containing a number between 1 and 1000 and SI prefix.
    """
if x == 0:
    return '0  '
l = math.floor(math.log10(abs(x)))
if abs(l) > 24:
    l = sign(l, value=24)
div, mod = divmod(l, 3 * dimension)
return '%.3g %s' % (x * 10 ** (-l + mod), ' kMGTPEZYyzafpn\xc2\xb5m'[div])
Pretty-printing physical quantities with automatic scaling of SI prefixes