(Lua) 导入/要求数学模块不起作用

我有一些代码在这里:

require "math"

local dozenDonuts
local oneDonut
local function roundToFirstDecimal(t)
    return math.round(t*10)*0.1
end

当我运行上面的代码时,我得到以下错误:

lua: f:\my codes\donut.lua:6: attempt to call field 'round' (a nil value)
stack traceback:
    f:\my codes\donut.lua:6: in function 'roundToFirstDecimal'
    f:\my codes\donut.lua:17: in main chunk
    [C]: ?

round不是math的属性吗?如何舍入到第一个小数?

点赞
用户107090
用户107090

math.round不是标准Lua数学库的一部分。但编写它很简单:

function math.round(x)
    return math.floor(x+0.5)
end
2020-02-01 12:00:43