def to_fixed_width(n, max_width, allow_overflow=True, do_round=True):
    """Format a float to a fixed-width string.

    Args:
      - n (float):              number to format
      - max_width (int):        width of output string
      - allow_overflow (bool):  allow the result to be larger than max_width for
                                large n? Raises OverflowError if False
      - do_round (bool):        Round final digit?
    Returns:
        (str)   string representation of n, either padded or rounded to have
                max_width characters
    """
    if do_round:
        for i in range(max_width - 2, -1, -1):
            str0 = '{:.{}f}'.format(n, i)
            if len(str0) <= max_width:
                break
    else:
        str0 = '{:.42f}'.format(n)
        int_part_len = str0.index('.')
        if int_part_len <= max_width - 2:
            str0 = str0[:max_width]
        else:
            str0 = str0[:int_part_len]
    if not allow_overflow and len(str0) > max_width:
        raise OverflowError(
            'Impossible to represent in fixed-width non-scientific format')
    return str0
