def remove_extra_digits(x, prog):
    """
    Remove extra digits
    x is a string,
    prog is always the following '_sre.SRE_Pattern':
    prog = re.compile("\\d*[.]\\d*([0]{5,100}|[9]{5,100})\\d*\\Z").
    However, it is compiled outside of this sub-function
    for performance reasons.
    """
    if not isinstance(x, str):
        return x
    result = prog.match(x)
    if result:
        decimals = result.string.split('.')[1]
        result = result.string
        if decimals[-3] == '0':
            result = x[:-2].rstrip('0')
        if decimals[-3] == '9':
            result = x[:-2].rstrip('9')
            try:
                last_digit = int(result[-1])
                result = result[:-1] + str(last_digit + 1)
            except ValueError:
                result = float(result[:-1]) + 1
        return result
    return x
