import math


def engFormat(f):
    """Format a number in engineering format, where the exponent is a multiple of 3"""
    if f == 0.0:
        value = 0.0
        exponent = 0
    else:
        exponent = math.log10(-f if f < 0 else f)
        if exponent < 0:
            exponent = -int(math.ceil(-exponent))
        else:
            exponent = int(math.floor(exponent))
        for i in range(3):
            if (exponent % 3) == 0:
                break
            exponent = exponent - 1
        value = f * 10 ** -exponent
    # Choose a format to maintain the number of useful digits we print.
    if abs(value) < 10:
        fmt = "%6.3f%s"
    elif abs(value) < 100:
        fmt = "%6.2f%s"
    else:
        fmt = "%6.1f%s"

    return fmt % (value, ("" if exponent == 0 else "e%d" % exponent))