def convert_length_to_km(length):
    """Convert length like 1000 to '1' or 30 to '30M'"""

    try:
        t_length = int(length)

        if t_length < 1000:
            length_str = str(t_length) + "M"
        else:
            length_str = "%g" % (float(t_length) / 1000.0)

    except ValueError:
        length_str = ""

    return length_str