I have a scenario that I need pass dynamic operators into loadstring
. This is where I found it and I don't understand.
Please see the following output:
> a = '3'
> b = '7'
> operator = '+'
> loadstring("return a" .. operator .. "b")()
10.0 -- Why do I get then with a decimal point.
> loadstring("return 3" .. operator .. 7)()
10 -- But this one is not?
Can anyone explain what's going on inside loadstring
since I thought I should get the same result?
The manual says this about arithmetic operators applied to strings:
if both operands are numbers or strings that can be converted to numbers (see §3.4.3), then they are converted to floats
Perhaps you want
loadstring("return " .. a .. operator .. b)()
instead of
loadstring("return a" .. operator .. "b")()