How to round numbers?

Hi, i’m wan’t to make grid placement system but i have a trouble with one thing,
how i can round number to nearest value that center aren’t 0.

for example we have our number X that we wan’t to round, then we have number Y that is our multiplier (when Y is 4 and X is 2 we wan’t to round X to Y) but with center of number Z

clear, i don’t know how to do this:


as you can see i have big problem with rounding to value that is continuation of Z+Y

for example, if our X is 5 and we wan’t round it to value on our specific number line, we rounding it to 3 instead of 4 , but if we have 9 for example , we wan’t to round this to 7 because 7 = 3+4 , anyone know how to do this like operation?

1 Like
math.floor(4.55)

Using math.floor() rounds up numbers for you at a pretty easy rate.

2 Likes

i know how to do that, but i need to round specific value, i need to round for example 2 to multiples of 3+4 or 3-4

For this example I used 4 as the grid size, but you can use whatever you want by making the first number half the second number.

If value < 2 then 
    value = 0
else
    value = 4
end

You could also do a calculation to divide it by 4, round it, then multiply it by 4 to get the same result.
Also @hollywoodleaks they now have math.round instead of math.floor to properly round decimal numbers.

2 Likes

If you want to make a specific grid you can use this:

local GridSize = 5

local function GetBlockPosition(PositionOf)
   return Vector3.new(math.floor(PositionOf.X/GridSize)*GridSize,math.floor(PositionOf.Y/GridSize)*GridSize, math.floor(PositionOf.Z/GridSize)*GridSize)
end
1 Like

Do you only want values that are multiples of 2 with an offset of 1?

local grid_size = 2
local offset = 1
math.round(x / grid_size) * grid_size + offset
3 Likes

okay, soo i tell it clearly, this is example:

we have X = 2 and we have a number line that starts at 3 our step is 4

then we wan’t round 2 to nearest value that are multiple of 4 but it’s multiple on this line

soo if we rounding 2 down we round 2 to -1 because 3 - 4 = -1
or maybe when we have 12 we round this down to 11 because 3+4+4 = 11

1 Like

This worked, thank you, it’s soo easy, it’s work in 5 trials soo, i’m will use it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.