I have a volume gui thing in my game, and I’d just like the number of the volume to be a float instead of just an integer, because having a 5 instead of a 5.0 is weirdly bothering me.
I could always just use string manipulation to get it, but I’m wondering if there’s an easier way.
Yep, I’m just telling him what you meant since he didn’t answer your question. If you just want to tack on .0 if there’s no fractional part, you can check if math.modf returns a second number and, if not, concatenate .0 to the end. Example:
local function toFloat(number)
local int, frac = math.modf(number)
if frac == 0 then
return int .. ".0" -- if there's no fractional part then shove on .0
end
return number -- if it returns frac it's already a float.
end
It might be better to use string manipulation, but this is probably the closest you can get without using that.
Lua has a single numerical type, Lua numbers. There is no distinction between integers and floats. So we always convert Lua numbers into integer replies, removing the decimal part of the number if any. If you want to return a float from Lua you should return it as a string , exactly like Redis itself does (see for instance the ZSCORE command).