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:

print(tostring(10)) -- "10"
print(tostring(10.0)) -- "10.0"

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 as 2.) You should always use an explicit format when you need a specific format for numbers.

Leave a Comment