We can use Ruby to find the floor of a number by using the floor method. The floor Ruby method rounds a number down to the nearest integer.
The above code would return 4, as it would round the number
4.653 down to 4.
Note that we can also pass the floor 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.floor num2 = 4.653.floor(2) puts num1 puts num2 #Output: 4 4.65
One calculation that is very easy to perform in Ruby is finding the floor of a number.
We can find the floor of a number easily with the Ruby floor method.
Below are some examples of how to use floor in Ruby to round down and find the floor of numbers.
puts 5.2.floor puts 10.867986.floor(3) puts 43.5.floor(1) puts -12.3.floor puts -0.3.floor #Output: 5 10.867 43.5 -13 -1
If you want to round up and get the ceiling of a number in Ruby, you can use the ceil method. To round to the nearest integer in Ruby, we can use the round method.
Hopefully this article has been beneficial for you to learn how to get the floor in Ruby of a number by using the Ruby floor method.
Leave a Reply