floorTo method

  1. @Possible({RangeError})
  2. @useResult
int floorTo(
  1. int factor
)

Floors this to the closest multiple of factor.

The returned int may underflow if sufficiently small.

Contract

Throws RangeError if factor <= 0.

Example

12.floorTo(10); // 10

22.floorTo(10); // 20

25.floorTo(10) // 20

1.floorTo(-5); // throws RangeError

Implementation

@Possible({RangeError})
@useResult int floorTo(int factor) => switch(factor) {
  < 1 => throw RangeError.range(factor, 1, null, 'factor'),
  _ => this - (this % factor),
};