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

Is rounding numbers to the nearest factor of _ possible?

Asked by 6 years ago

I'm trying to do a thing for example if the number is 1, it would round the the factor of 4 which would be 4. Or for example 7, it would round to the nearest factor of 8. Is there A way to do this within ROBLOX scripting?

2 answers

Log in to vote
0
Answered by 6 years ago
local function roundNumberToFactor(number, factor)
    return number + (factor - (number % factor))
end

This rounds it up to the highest nearest factor of the number. If you want it to take it to the absolute nearest factor of the number:

local function roundNumberToFactor(number, factor)
    if(number % factor > factor/2) then
        return number + (factor - (number % factor))
    else
        return number - (number % factor)
    end
end

However this would take 1 to 0 and not 4 as you asked, so I don't know if this helps or not.

Ad
Log in to vote
0
Answered by 6 years ago

math.floor()

0
"Returns a number rounded down (towards negative infinity) to the nearest integer." That number is the nearest one, not another number like 4.. :thonk: hunterthegreat100 18 — 6y
0
Sorry read that wrong. :( User#19524 175 — 6y

Answer this question