def FormatToMinDP(val, max_dp):
    """ Format val as a string with the minimum number of required decimal places, up to a maximum of max_dp. """

    num_dp = 0

    while True:
        pow = -1 * num_dp

        if val % (10 ** pow) == 0.0 or num_dp == max_dp:
            break

        num_dp += 1

    return "{{0:.{0}f}}".format(num_dp).format(val)