What is floor() in Ruby?

The floor() function in Python returns the next largest number less than or equal to a number set as a parameter. Figure 1 below shows the mathematical representation of the floor() function.

Figure 1: Mathematical representation of floor() function

Syntax

num.floor(digits)
## where num is a number to round down

Parameter

This function requires n digits to which the precision of decimal digits is kept. It is optional, and if its value is omitted, then its default value is 0.

1.234567.floor(2)   => 1.23
34577.78.floor(-2)  => 34500 

Return value

The floor() function returns the next largest number less than or equal to the number set as a parameter.

Example

#positive integer
num1=10
print "num1.floor() : ", num1.floor(), "\n"
#negative integer
num2=-10
print "num2.floor() : ", num2.floor(), "\n"
#positive float value
num3=2.1
print "num3.floor() : ", num3.floor(), "\n"
#negative float value
num4=-2.1
print "num4.floor() : ", num4.floor(), "\n"

Free Resources