math.floor() function in Lua programming

LuaServer Side ProgrammingProgramming

There are several occurrences when we want to get the floor value of an integer to round it off and then use that value later on. The floor value of a number is the value that is rounded to the closest integer less than or equal to that integer. Lua provides us with a math.floor() function that we can use to find the floor value of a number.

Example

Let’s consider a simple example where we will make use of the math.floor() function in Lua −

 Live Demo

a = math.floor(3.3)
b = math.floor(7.1)
print(a)
print(b)

Output

3
7

It should be noted that if we try to find floor of a number which is already the closest integer to itself then we will get the same number as the output.

Example

Consider the example shown below −

 Live Demo

c = math.floor(8)
print(c)

Output

8

We can also pass negative numbers in the math.floor() function as an argument.

Example

Consider the example shown below −

 Live Demo

d = math.floor(-3.3)
print(d)

Output

-4
raja
Published on 19-Jul-2021 11:46:19
Advertisements