Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is rounding numbers to the nearest factor of _ possible?

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

✓ Accepted answer
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.

math.floor()

2 comments

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:

0

Sorry read that wrong. :(

This discussion is preserved as it was. New answers, comments, and votes are closed.