How to round a number to the nearest 0.5?

Hey! I have a placement system and I need a way to round a number to the nearest 0.5, yes I’ve already searched this topic and found a few posts that really didn’t help at all. So here’s an example of what I’m trying to accomplish, so here’s a few examples

etc, I haven’t been able to find a way to do this though. Any ideas?

You can use math.round

local number = 1.0
print(math.round(number)) --> 1

use this:

function SnapPosition(original, size)
	local snapped =
	snapped =
		math.floor(original.X / size + 0.5) * size,
		math.floor(original.Y / size + 0.5) * size,
		math.floor(original.Z / size + 0.5) * size
	return snapped

original : vector3
size : float (decimals or ints)

1 Like

math.round() isn’t what I’m looking for, it needs to go to the nearest 0.5. So for example:

0.5 - 0.5
0.4 - 0.5
0.1 = 0

THANK YOU SO MUCH! Literally what I’ve been looking for, have a great day!

another way would be this

function SnapPosition(original, size)
        math.round(original.X / size) * size,
        math.round(original.Y / size) * size,
        math.round(original.Z / size) * size

don’t see why math.round isn’t used here