Lua Semicolon Conventions

Semi-colons in Lua are generally only required when writing multiple statements on a line. So for example: Alternatively written as: Off the top of my head, I can’t remember any other time in Lua where I had to use a semi-colon. Edit: looking in the lua 5.2 reference I see one other common place where … Read more

Lua: Rounding numbers and then truncate

Which is the best efficient way to round up a number and then truncate it (remove decimal places after rounding up)? for example if decimal is above 0.5 (that is, 0.6, 0.7, and so on), I want to round up and then truncate (case 1). Otherwise, I would like to truncate (case 2)

How to iterate through table in Lua?

To iterate over all the key-value pairs in a table you can use pairs: outputs: Edit: Note that Lua doesn’t guarantee any iteration order for the associative part of the table. If you want to access the items in a specific order, retrieve the keys from arr and sort it. Then access arr through the … Read more

Lua – Number to string behaviour

In Lua 5.2 or earlier, both tostring(10) and tostring(10.0) result as the string “10”. In Lua 5.3, this has changed: That’s because Lua 5.3 introduced the integer subtype. From Changes in the Language: The conversion of a float to a string now adds a .0 suffix to the result if it looks like an integer. (For instance, the float 2.0 will be printed as 2.0, not … Read more

You could use math.floor(x) From the Lua Reference Manual: Returns the largest integer smaller than or equal to x.