The round() method can be used to round a number to a specified number of decimal places in Ruby. We can use it without a parameter (round()) or with a parameter (round(n)).
n here means the number of decimal places to round it to.
# default rounding
float_value.round
# to n decimal places
float_value.round(n)
float_value: This is the float value we want to round.
n: Represents the number of decimal places we want to round the float_value to.
The value returned is a float rounded to the nearest value with a precision of n decimal digits or 0 digit if no parameter is passed.
# create some float valuesf1 = 2.3333545f2 = 1.4445567f3 = 100.12711f4 = -0.8889# round float values# and print resultsputs f1.round # to 0 decimal placeputs f2.round(3) # to 3 decimal placesputs f3.round(1) # to 1 decimal placeputs f4.round(2) # to 2 decimal places
round() method on the already defined float values and print the results.