1

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?

averwhy
  • 142
  • 14
  • 2
    does this answer your question? https://stackoverflow.com/questions/18313171/lua-rounding-numbers-and-then-truncate – Nifim Jan 31 '20 at 18:15

1 Answers1

1

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
lhf
  • 70,581
  • 9
  • 108
  • 149