Actions
Feature #16021
closedfloor/ceil/round/truncate should accept a :step argument
Feature #16021:
floor/ceil/round/truncate should accept a :step argument
Status:
Feedback
Assignee:
-
Target version:
-
Description
These rounding methods currently accept a number of (decimal) digits, but a more general mechanism would allow rounding to the nearest ΒΌ, multiple of 5, etc.
Equivalent to e.g. ((num / step.to_f).round * step)
12.3456.floor(step: 0.2) #=> 12.2
12.3456.round(step: 0.2) #=> 12.4
12.3456.floor(step: 0.2) #=> 12.4
12.3456.floor(step: 0.2) #=> 12.2
IMHO this should also apply to Time#floor/round/ceil
Time.now.floor(step: 3600) #=> current hour
Time.now.round(step: 3600) #=> nearest hour
Time.now.ceil(step: 3600) #=> next hour
We can also consider that instead of :step
, :by
or :to
might be quite readable.
12.3456.round(by: 0.2) #=> 12.4
12.3456.round(to: 0.2) #=> 12.4
Actions