Search code examples
pythonmathclamp

Clamping floating numbers in Python?


Is there a built-in function for this in Python 2.6?

Something like:

clamp(myValue, min, max)

Solution

  • There's no such function, but

    max(min(my_value, max_value), min_value)
    

    will do the trick.