Skip to content

Function: clamp()

ts
function clamp(
   value, 
   lower, 
   upper): number;

Clamps a number within the inclusive lower and upper bounds. 将数字限制在指定的上下边界内。

Parameters

ParameterTypeDescription
valuenumberThe number to clamp / 要限制的数字
lowernumberThe lower bound / 下边界
uppernumberThe upper bound / 上边界

Returns

number

The clamped value / 限制后的值

Example

typescript
clamp(10, 0, 5) // => 5
clamp(-10, 0, 5) // => 0
clamp(3, 0, 5) // => 3

Since

1.0.0

Released under the MIT License.