def dformat(val, ndigits, slen):
    """
    Right-pads a decimal with spaces such that there are max_exp number of characters after the dot
    and the complete string is max_len characters in width.
    """

    digits = ndigits + val.as_tuple().exponent if val.as_tuple().exponent < 0 else ndigits + 1
    return f"{format(val, 'f') + ' '*(digits):>{slen}}"