def scale(val, inRange, outRange, decimals=0):
    """
    Scale the given value from one range to another
    """
    if val is None:
        return None

    result = (
        ((val - inRange[0]) / (inRange[1] - inRange[0]))
        * (outRange[1] - outRange[0])
        + outRange[0]
    )

    if decimals == 0 or result % 1 == 0:
        return round(result)
    else:
        return round(result, decimals)