math.ceil() function in Lua programming

LuaServer Side ProgrammingProgramming

There are several occurrences when we want to get the ceil value of an integer to round it off and then use that value later on.

The Ceiling value of a number is the value that is rounded to the closest integer greater than or equal to that integer.

Lua provides us with a math.ceil() function that we can use to find the ceil value of a number.

Example

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

 Live Demo

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

Output

4
8

Example

It should be noted that if we try to find ceil of a number which is already the closest integer to itself, then we will get the same number as the output. Consider the example shown below −

 Live Demo

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

Output

8

Example

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

Consider the example shown below −

Live Demo

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

Output

-3
raja
Published on 19-Jul-2021 11:44:53
Advertisements