✓ Accepted answer
There isn't a super nice way to do this in Lua, unfortunately.
Here are two options. One based on processing the number as a number, the other by processing the number as a string.
Math
We can use modulus to find the thousandths+ part of the number, and remove that. If we want to round rather than truncate, we would add five thousandths to the number before doing this:
function truncate(x, places)
return x - x % 10^(-places)
end
function round(x, places)
return truncate(x + 10 ^ (-places) / 2, places)
end
Strings
We can turn the number into a string, then chop off the places too many after the decimal point. This can't (easily), only truncate.
x = tostring(1.99)
x = x:gsub("%.(%d%d)%d+",".%1");
A cool property of the second one is that you can use it on full segments of text, not just the number itself.
1 comment
Funny because I was going to ask the same exact question :\