Skip to content

Home

Clamp number

Clamps num within the inclusive range specified by the boundary values.

def clamp_number(num, a, b):
  return max(min(num, max(a, b)), min(a, b))

clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1

More like this

Start typing a keyphrase to see matching snippets.