Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to round number to nearest multiple of 15?

Asked by 4 years ago

So there is this grid placing system but the only thing it misses is for it to snap to a unit which are 15 studs squared each. Since i only have the coordinates of the mouse.hit.p and those don't conform to the grid, i need to know how to round those to the nearest multiple of 15, positive or negative. thanks.

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Here's a simple formula:

I'm gonna use 15 here to fit your example.

--replace number with the variable you're trying to round
local roundednumber = (math.floor((number + 7.5)/15))*15

This works with all numbers. In case you decide to change to a different rounding factor, simply swap the 7.5 with half of the number you're rounding to, for instance if you're rounding to the nearest multiple of 40 you do:

--replace number with the variable you're trying to round
local roundednumber = (math.floor((number + 20)/40))*40

There's not much to it besides this really. Hope this helps!

I'm working on a grid placement system too btw, so if you can, please help me out :) https://scriptinghelpers.org/questions/117394/how-should-i-make-a-grid-building-system

0
thanks! R_LabradorRetriever 198 — 4y
Ad

Answer this question