【问题标题】:(Lua) Importing/requiring math modules doesn't work(Lua) 导入/需要数学模块不起作用
【发布时间】:2020-01-31 17:50:13
【问题描述】:

我这里有一些代码:

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 lua


【解决方案1】:

math.round 不是标准 Lua 数学库的一部分。不过写起来很简单:

function math.round(x)
    return math.floor(x+0.5)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多