From: daniel@...42.com Date: 2019-07-26T13:31:09+00:00 Subject: [ruby-core:93932] [Ruby master Feature#16021] floor/ceil/round/truncate should accept a :step argument Issue #16021 has been updated by Dan0042 (Daniel DeLorme). shevegen (Robert A. Heiler) wrote: > (Not the same, I know, but my point is mostly that the second usage is > much easier to understand from a glance alone.) Then what about making them equivalent so it's easier to compare which is more understandable. ``` ruby 12.3456.floor(step: 0.0002) ``` versus ``` ruby (12.3456 / 0.0002).floor * 0.0002 ``` IMHO the intent of the first one is obvious even without reading the `floor` documentation (but of course YMMV). If you think the second one is more understandable then my proposal has no leg to stand on. Although as shyouhei pointed out, the pitfalls of float arithmetic mean that solving those pitfalls gives this proposal more value than just syntactic sugar alone. ---------------------------------------- Feature #16021: floor/ceil/round/truncate should accept a :step argument https://bugs.ruby-lang.org/issues/16021#change-80061 * Author: Dan0042 (Daniel DeLorme) * Status: Feedback * Priority: Normal * Assignee: * Target version: ---------------------------------------- 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)` ``` ruby 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 ``` ruby 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` might be quite readable. ``` ruby 12.3456.round(by: 0.2) #=> 12.4 ``` -- https://bugs.ruby-lang.org/ Unsubscribe: