Updated April 20, 2023
Definition of Lua Round
Lua provides us some round functions which is used to round off the numerical values. There is no function named as round rather we have some are options like the floor, ceil from the math library. Round functions help us to get the more accurate number without decimal included, it can move towards positive infinity or negative infinity depends upon the functions we have used to round off the value in Lua. The round function is used to round off the lng decimals numbers to some user-readable form an approx value. These round functions are available inside the lua inbuilt library, we do not require to use any external library for this. In the coming section of the tutorial, we will learn more about the round function in detail to understand and implement this while programming in Lua.
Syntax
As discussed we do not have a specific function called as round() in lua but still, we have a math library from lua which helps us to achieve the desired result. Let’s take a look at its syntax how we can use and implement it in our program see below;
1. ceil
math.ceil(your-_variable to be round off)
2. floor
math.floor(your-_variable to be round off)
As you can see in the above lines of syntax we have two options available for round off values in lua. Let’s take an practice example to understand it better see below;
Example:
math.ceil(10.6789)
math.floor(10.6789)
In the coming section of the tutorial, we will discuss the internal working of round functions available in lua for better usage for beginners.
How Round Function Works in Lua?
As we already know that in Lua we have two options to round off values or numbers in lua without using any external library. Lua provides us Math library support to handle the rounding of values efficiently. We can use any method from the math library, just keep in mind we have to use the ‘math’ keyword to call them and use them. In this section we will see the internal working of round function in Lua see below;
1. Floor
This function is used to round off the value of a big decimal. This function is provided by the math library, which is very easy to use in the program, we do not require writing any import statement to use this, we can directly call the floor function on math. When we use floor function to round off any value it will give us the next number to negative infinity, suppose if you have one number say 0.5 it will give us 0 in this case, that means one number below, Let’s see its usage;
Method Signature:
math. floor(number) : As you can see in the method signature, it takes one parameter inside the function, which represents the value to be round off. It will always return us the number without decimal. Let’s take an simple example :
Example:
math.floor(10.4567)
This way we can use it, we can pass the variable directly inside it, this will give us the correct value after calculation.
2. Ceil
This function is also used to round off the value to the nearest value possible. This function is also provided by the math library in lua, for this function to use we do not require any import statement while using it in our program. When we use ceil method and pass the variable inside it, it will always give us the next value to positive infinity, which means a number greater. Let’s suppose we have one value which is to be round off, that is ‘0.5’ then in space of ceil it will return us 1 instead of 0 like in case of floor function above.
Method Signature:
math.ceil(number): As you can see in the method signature, it takes one parameter inside the function, which represents the value to be round off. It will always return us the number without decimal. Let’s take a simple example :
Example:
math.ceil(10.4567)
Points to be remembered while using round function in lua;
- There is no function named as round function in lua, we have to use the math library to achieve it.
- We have floor and ceil functions.
- One will return us the value toward negative infinity and one will return us the value towards positive infinity.
Examples of Lua Round
In the below example we are doing a comparison among both functions what will they return if we try to pass the same value using different functions in lua. It will showcase the difference among them and now you decide which function to be used.
Example #1
In this example, we are using the floor method to round the values to next whole number. This is an sample example for beginners to start with rounding values in lua using math functions.
Code:
print("Demo to round values in lua ")
val1 = math.floor(0.5)
val2 = math.floor(20.45667)
val3 = math.floor(13.6547)
val4 = math.floor(0.789)
val5 = math.floor(5.872)
print("print result here ..")
print("first value is ::" , val1)
print("second value is ::" , val2)
print("third value is ::" , val3)
print("fourth value is ::" , val4)
print("fifth value is ::" , val5)
Output:
Example #2
In this example, we are using ceil method to round the values to the next whole number. This is a sample example for beginners to start with rounding values in lua using math functions.
Code:
print("Demo to round values in lua ")
val1 = math.ceil(0.5)
val2 = math.ceil(20.45667)
val3 = math.ceil(13.6547)
val4 = math.ceil(0.789)
val5 = math.ceil(5.872)
print("print result here ..")
print("first value is ::" , val1)
print("second value is ::" , val2)
print("third value is ::" , val3)
print("fourth value is ::" , val4)
print("fifth value is ::" , val5)
Output:
Conclusion
By using the round function, we can remove the decimal part of the numbers, it will return us the next whole number. These functions are very easy to use and implement in the program. By the use of this wean represent the value to the user in readable form and perform a calculation to get the result.
Recommended Articles
We hope that this EDUCBA information on “Lua Round” was beneficial to you. You can view EDUCBA’s recommended articles for more information.