def FormatValue(val, maxdigits=None):
    """Format a float to fit in at most ``maxdigits[0]`` spaces with maxdigits[1] after decimal.
    Note that this code has been hacked from FormatSigFigs and may have unused sections. 

    :param float val: number to be formatted.

    :param list maxdigits: the number of digits, places after decimal and 'f' or 'g' to be used for display of the
      number (defaults to [10,2,'f']).

    :returns: a string with <= maxdigits characters (usually).  
    """
    if 'str' in str(type(val)) and (val == '?' or val == '.'):
        return val
    if maxdigits is None:
        digits = [10, 2, 'f']
    else:
        digits = list(maxdigits)
    if len(digits) == 2:
        digits.append('f')
    if not val:
        digits[2] = 'f'
    fmt = '{:' + str(digits[0]) + '.' + str(digits[1]) + digits[2] + '}'
    string = fmt.format(float(val)).strip()
    if len(string) <= digits[0]:
        if ':' in string:
            string = str(val)
        if digits[1] > 0:
            string = string.rstrip('0')
            if string[-1] == '.':
                string += '0'
        return string
    if val < 0:
        digits[0] -= 1
    decimals = digits[0] - digits[1]
    if abs(val) > 1e+99:
        fmt = '{' + ':{:d}.{:d}g'.format(digits[0], digits[0] - 6) + '}'
    elif abs(val) > 1000000000.0:
        fmt = '{' + ':{:d}.{:d}g'.format(digits[0], digits[0] - 5) + '}'
    elif abs(val) < 10 ** (4 - decimals):
        decimals = min(digits[0] - 5, digits[1])
        fmt = '{' + ':{:d}.{:d}g'.format(digits[0], decimals) + '}'
    elif abs(val) >= 10 ** (decimals - 1):
        decimals = max(0, digits[0] - 5)
        fmt = '{' + ':{:d}.{:d}g'.format(digits[0], decimals) + '}'
    elif abs(val) < 1:
        decimals = min(digits[0] - 3, digits[1])
        fmt = '{' + ':{:d}.{:d}f'.format(digits[0], decimals) + '}'
    else:
        decimals = digits[0] - 6
        fmt = '{' + ':{:d}.{:d}g'.format(digits[0], decimals) + '}'
    try:
        return fmt.format(float(val)).strip()
    except ValueError:
        print('FormatValue Error with val,maxdigits,fmt= %f %d %s' % (val,
            maxdigits, fmt))
        return str(val)
