Round up to the nearest tenth?

If you have ActiveSupport available, it adds a round method:

3.14.round(1) # => 3.1
3.14159.round(3) # => 3.142

The source is as follows:

def round_with_precision(precision = nil)
  precision.nil? ? round_without_precision : (self * (10 ** precision)).round / (10 ** precision).to_f
end

Ruby's round method can consume precisions:

10.38.round(1) # => 10.4

In this case 1 gets you rounding to the nearest tenth.


To round up to the nearest tenth in Ruby you could do

(number/10.0).ceil*10

(12345/10.0).ceil*10 # => 12350


This works in general:

ceil(number*10)/10

So in Ruby it should be like:

(number*10).ceil/10.0