Skip to content

Instantly share code, notes, and snippets.

@sachin21
Created February 4, 2018 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sachin21/4085cc9dfee12ae3356f1d73a034b9e7 to your computer and use it in GitHub Desktop.
Save sachin21/4085cc9dfee12ae3356f1d73a034b9e7 to your computer and use it in GitHub Desktop.
[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
Copy link
Author

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
Copy link

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
Copy link
Author

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