averwhy
averwhy

Reputation: 142

(Lua) Importing/requiring math modules doesn't work

I have some code here:

require "math"

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

When I run the code above, I get the following error:

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]: ?

Is round not an attribute of math? How can I round to the first decimal?

Upvotes: 1

Views: 3647

Answers (1)

lhf
lhf

Reputation: 72312

math.round is not part of the standard Lua math library. But it is simple to write:

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

Upvotes: 1

Related Questions