take the floor of a number in ruby

In Ruby, you can use the floor method to round a number down to the nearest integer. Here's an example:

main.rb
x = 3.14159
y = x.floor # y is now 3
37 chars
3 lines

In this example, x.floor returns the integer 3, which is the largest integer less than or equal to x. If x was negative, floor would round it towards negative infinity.

You can also use floor with a negative argument to round to a specific number of decimal places. For example:

main.rb
x = 3.14159
y = x.floor(2) # y is now 3.14
43 chars
3 lines

In this example, x.floor(2) returns the number 3.14, which is the largest number less than or equal to x with two decimal places.

gistlibby LogSnag