(removed by post author)

(removed by post author, until later notice)

To round you may use math.round() as it is the same as math.floor(x + .5).

I believe using modulo may be an appropriate approach, it will give you what is missing to make it evenly divisible.

If you have an increment of 2 then only numbers divisible by 2 would be allowed. I think you might be able to do something like this:

function f(inc, val)
	val = math.round(val)
	local diff = val % inc
	return math.round(val + diff/2)
end

print(f(2, 2.5)) -- Prints 4

This is not exactly what you wanted, but this is a perhaps a good starting point for you. Currently it will round anything from from .5 and up to the next interval, but you can probably fix this with some minor adjustments.

1 Like

Not exactly true. math.round() rounds up or down while math.floor() will only round down

Yes, but adding .5 to any value inside of it will round it up or down.

math.floor(x + .5) -- Expression
-- Examples:
math.floor(.3 + .5) -- = 0.8 => 0 [math.round(0.3) => 0]
math.floor(.4 + .5) -- = 0.9 => 0 [math.round(0.4) => 0]
math.floor(.5 + .5) -- = 1 => 1 [math.round(0.5) => 1]
math.floor(.9 + .5) -- = 1.4 => 0 [math.round(0.9) => 1]

(removed by post author, until later notice)

1 Like
local function RoundNumberToNearestMultipleOfN(Number, N)
	return math.floor(Number / N) * N
end

print(RoundNumberToNearestMultipleOfN(15, 7)) --14
print(RoundNumberToNearestMultipleOfN(10, 3)) --9

Shouldn’t 2.5 round down to 2 not up to 4? Assuming you want to snap a value to a grid of values.

Check my reply above.

Yes, it should.

However, since 4.5 will round to 6 it will still be 2 units of space, just offset in a dumb way.