floor()
In Lua, the math.floor()
function is a standard function that is part of the Lua math
library. The math.floor()
function is used to round down a given value to its closest integer value that is less than or equal to the given value.
Note:
math.floor()
always returns a value equal to or less than the given value. It does not round up under any circumstances. For example,math.floor(2.999)
returns 2 as that is the closest integer value that is less than or equal to 2.999. To round numbers in either direction, usemath.round()
.
Syntax
As floor()
is a method that is a part of the standard Lua math
library, it must be called as math.floor()
.
math.floor(mycoolvalue) -- Returns the closest integer value that is less than or equal to mycoolvalue
Example 1
To find the closest integer less than or equal to 1.234:
print(math.floor(1.234))
This results in the following output:
1
Example 2
It is important to remember that math.floor()
does not perform rounding, as in the following example:
print(math.floor(2.999))
This will yield the following output:
2
Example 3
When working with negative numbers, it is important to remember that math.floor()
returns the closest integer less than or equal to a given value:
print(math.floor(-4.56))
This results in the following output:
-5
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours