take the ceiling of a number in ruby

In Ruby, you can use the ceil method to round a float or decimal number up to the nearest integer, also known as the ceiling.

Here's an example:

main.rb
num = 3.14159
ceil_num = num.ceil
puts ceil_num # Output: 4
60 chars
4 lines

In this example, we first declare a variable num and assign it the value of 3.14159. We then call the ceil method on num to round it up to the nearest integer, which is 4. Finally, we use the puts method to print the value of ceil_num to the console.

Note: The ceil method returns a float value so if you want to get an integer value after taking the ceiling, you should use the ceil.to_i expression instead.

related categories

gistlibby LogSnag