Ruby 2.0 - Rounding an integer to the nearest multiple of 10 Ruby 2.0 - Rounding an integer to the nearest multiple of 10 ruby ruby

Ruby 2.0 - Rounding an integer to the nearest multiple of 10


Integer#round has the functionality.

You pass a negative number to round to represent which 10's digit you'd want to round to. For example:

Round to the nearest 10:

55.round(-1) # => 60

To round to the nearest 100:

550.round(-2) # => 600


You can just divide by 10, round, then multiply by 10:

nearest = (x/ 10).round * 10