I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min
Math.min
OP asks for this implementation in a standard library:
int ensureRange(int value, int min, int max) { return Math.min(Math.max(value, min), max); } boolean inRange(int value, int min, int max) { return (value>= min) && (value<= max); }
A pity the standard Math library lacks these