Page 1 of 1

(Math Help) How do I round to the nearest 50?

Posted: Fri Jun 14, 2019 7:16 pm
by icekiller8002
I.E.
23 = 0
27 = 50
56 = 50
79 = 100
122 = 100
143 = 150
178 = 200
994 = 1000

Basically, if the last two digits are closer to 100, then it rounds it to 100. Otherwise, if the last two digits are closer to 50, then it rounds it to 50. Or if the last two digits are closer to zero, then it rounds it to zero.

Re: (Math Help) How do I round to the nearest 50?

Posted: Fri Jun 14, 2019 7:26 pm
by TheHUG
first thing that comes to mind is:

Code: Select all

function roundnearest(num, to)
    local rem = num % to
    if rem < (to / 2) then
        return num - rem
    else
        return num - rem + to
    end    
end
(then you call it with '50' as the second argument)
edit: messed it up, need sleep. fixed.

Re: (Math Help) How do I round to the nearest 50?

Posted: Fri Jun 14, 2019 10:38 pm
by raidho36
You can try the following:

Code: Select all

function roundTo ( number, multiple )
  return math.floor ( ( number / multiple ) + 0.5 ) * multiple
end