forceBetween method

double forceBetween(
  1. double from,
  2. double to
)

Clamps this value to be within the range from to to.

Returns:

  • from if this value is less than from
  • to if this value is greater than to
  • This value if it's within the range

Example:

5.0.forceBetween(0.0, 10.0); // 5.0
(-5.0).forceBetween(0.0, 10.0); // 0.0
15.0.forceBetween(0.0, 10.0); // 10.0

Implementation

double forceBetween(double from, double to) {
  if (this < from) {
    return from;
  }

  if (this > to) {
    return to;
  }

  return this;
}