为什么我在 Lua 中使用 loadstring 得到了小数点
why did I get a decimal point by using loadstring in Lua
我有一个场景需要将动态运算符传递给 loadstring
。这是我找到它的地方,我不明白。
请查看以下输出:
> 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?
任何人都可以解释一下 loadstring
里面发生了什么,因为我认为我应该得到相同的结果?
manual 关于应用于字符串的算术运算符是这样说的:
if both operands are numbers or strings that can be converted to numbers (see §3.4.3), then they are converted to floats
也许你想要
loadstring("return " .. a .. operator .. b)()
而不是
loadstring("return a" .. operator .. "b")()
我有一个场景需要将动态运算符传递给 loadstring
。这是我找到它的地方,我不明白。
请查看以下输出:
> 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?
任何人都可以解释一下 loadstring
里面发生了什么,因为我认为我应该得到相同的结果?
manual 关于应用于字符串的算术运算符是这样说的:
if both operands are numbers or strings that can be converted to numbers (see §3.4.3), then they are converted to floats
也许你想要
loadstring("return " .. a .. operator .. b)()
而不是
loadstring("return a" .. operator .. "b")()