clamp(X;Y;Z)
Returns Y
if X<Y
and
Z
if X>Z
. Otherwise, if the
"clamped" value X
is between Y
and
Z
, clamp(X;Y;Z)
returns
X
.
(Available as of version 18.37)
Syntax
clamp(X;Y;Z)
Input
Argument | Type | Description |
---|---|---|
X |
any simple type | The value to be "clamped". |
Y |
any simple type | The minimum allowed value. |
Z |
any simple type | The maximum allowed value. |
Return Value
Returns Y
if X<Y
, Z if
X>Z
, else X
. The return type is
the same as the type of X
, which may be integer,
bigint, or float (in which case Y
and
Z
must be a numeric type).
If the types of Y
and/or Z
differ from
the the type of X
, then Y
and
Z
will be coerced to the type of
X
or a string (in which case
Y
and Z
must also be a
string).
clamp(X;Y;Z)
is the equivalent of
if(X<Y;Y;X>Z;Z;X)
.
Sample Usage
value |
min |
max |
clamp(value;min;max) |
---|---|---|---|
2 | 1 | 3 | 2 |
2 | 3 | 1 | 1 |
1.5 | 1 | 3 | 1.5 |
1.5 | 2.5 | 3.1 | 2.5 |
1.5 | 1.2 | 3.1 | 1.5 |
'bat' | 'bar' | 'baz' | 'bat' |
'bag' | 'bar' | 'baz' | 'bar' |
'foo' | 'bar' | 'baz' | 'baz' |
3 | 'foo' | 'bar' | 'foo' |
3 | 4 | 'foo' | 4 |