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