Skip to content

Instantly share code, notes, and snippets.

@sachin21
Created Feb 4, 2018
Embed
What would you like to do?
[14] pry(main)> 1.0.step(2, 0.1).to_a
=> [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7000000000000002, 1.8, 1.9, 2.0]
@sachin21

This comment has been minimized.

Copy link
Owner Author

@sachin21 sachin21 commented Feb 4, 2018

Hmm. the answer is this?

1.0.step(2, 0.1).map(&:to_s).map { |float| BigDecimal(float).floor(1).to_f }
=> [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0]
@hanachin

This comment has been minimized.

Copy link

@hanachin hanachin commented Feb 5, 2018

(11/10r).step(2, 1/10r).map(&:to_f)
# => [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0]
@sachin21

This comment has been minimized.

Copy link
Owner Author

@sachin21 sachin21 commented Apr 11, 2020

Thanks for your answer!
It looks it is the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment