We can use Ruby to round a number to the nearest integer by using the round method. The round Ruby method rounds a number to the nearest integer.
The above code would return 5, as it would round the number
4.653 to the nearest integer, 5.
Note that we can also pass the round method an integer parameter that will be the number of decimal places that we want to round the number to.
Let’s see the output of both of these simple examples.
num1 = 4.653.round
num2 = 4.653.round(2)
One calculation that is very easy to perform in Ruby is rounding a number.
We can round a number easily with the Ruby round method.
Below are some examples of how to use round in Ruby to round a bunch of different numbers.
Hopefully this article has been beneficial for you to learn how to use the Ruby round method to round a number to the nearest integer.