且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Lua中将计算字符串转换为int

更新时间:2023-02-03 08:33:30

tonumber只能用于实数字符串,不能用于算术表达式.

tonumber can be used only on a string that is a real number, not an arithmetic expression.

您可以加载字符串并运行它:

You can load the string and run it:

x = "5 + 5"
func = assert(load("return " .. x))
y = func()
print(y)

在Lua 5.1中,使用loadstring代替load.

In Lua 5.1, use loadstring instead of load.