# 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 = Vector3.new()
snapped = Vector3.new(
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
end
``````

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
etc

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

another way would be this

``````function SnapPosition(original, size)
return Vector3.new(
math.round(original.X / size) * size,
math.round(original.Y / size) * size,
math.round(original.Z / size) * size
)
end
``````

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