This blog is part of our Ruby 2.4 series.

In Ruby, there are many methods available which help us to modify a float or integer value.

Ruby 2.3.x

In the previous versions of Ruby, we could use methods such as floor , ceil and truncate in following ways.

5.54 . floor #=> 5 5.54 . ceil #=> 6 5.54 . truncate #=> 5

Providing an argument to these methods would result in ArgumentError exception.

Ruby 2.4

Ruby community decided to come up with an option to add precision argument .

The precision argument, which can be negative, helps us to get result to the required precision to either side of the decimal point.

The default value for the precision argument is 0.

876.543 . floor ( - 2 ) #=> 800 876.543 . floor ( - 1 ) #=> 870 876.543 . floor #=> 876 876.543 . floor ( 1 ) #=> 876.5 876.543 . floor ( 2 ) #=> 876.54 876.543 . ceil ( - 2 ) #=> 900 876.543 . ceil ( - 1 ) #=> 880 876.543 . ceil #=> 877 876.543 . ceil ( 1 ) #=> 876.6 876.543 . ceil ( 2 ) #=> 876.55 876.543 . truncate ( - 2 ) #=> 800 876.543 . truncate ( - 1 ) #=> 870 876.543 . truncate #=> 876 876.543 . truncate ( 1 ) #=> 876.5 876.543 . truncate ( 2 ) #=> 876.54

These methods all work the same on Integer as well.