[ruby-core:93925] [Ruby master Feature#16021] floor/ceil/round/truncate should accept a :step argument

From: shyouhei@...
Date: 2019-07-26 02:17:16 UTC
List: ruby-core #93925
Issue #16021 has been updated by shyouhei (Shyouhei Urabe).

Status changed from Open to Feedback

Dan0042 (Daniel DeLorme) wrote:
> Equivalent to e.g. `((num / step.to_f).round * step)`
> 
> ``` ruby
> 12.3456.floor(step: 0.2) #=> 12.2
> ```

Can I ask you what do you expect for `12.3456.floor(step: 0.0002)` then?

----------------------------------------
Feature #16021: floor/ceil/round/truncate should accept a :step argument
https://bugs.ruby-lang.org/issues/16021#change-80055

* 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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next