Does java have a clamp function?

weston picture weston · May 20, 2013 · Viewed 57.4k times · Source

Suppose I have a value, I usually do this to "clamp" it to a range, here the range [0..1]. That is if it is below the range start, increase it to the range start, it above the range end, reduce it to the range end.

clampedValue = Math.max(0, Math.min(1, value));

Is there any built in function for clamping to a range?

Answer

Matt Ball picture Matt Ball · May 20, 2013

Is there any built in function for clamping to a range?

No.