# Rounding to a Specific Number

I feel dumb having to ask this, but how would I round to a specific number? I swear I used to know how, but I’m suddenly blanking.

Let’s say I want to round a number to the nearest multiple of 3. How would I go about that exactly? Is there a quick math operation, or would I have to write my own math function?

1 Like

To round to the nearest multiple of 3, divide by 3, round to the nearest 1, then multiply by 3

3 Likes
``````local number = 2.4

if number > 2 and number < 3 then
number = 3
end
``````

i think this works, i havent worked with math before

`math.ceil()` would produce the same result.

`print(math.ceil(2.4)) --3`

1 Like

well i guess i have learned something today!

But `math.ceil(1.4)` would produce 2. I wanted to round specifically by 3’s. @GolgiToad is the answer I was looking for. Thank you for the response though, and you as well @lolmansReturn.

I was responding to them, not the thread.

Oh, my bad. I wasn’t paying attention.

``````local function roundToNearestMultipleOfThree(number)
return if ((number / 3) - (math.floor(number / 3))) < 0.5 then (number - number % 3) else (number + (number % 3)/2)
end

local nearestMultiple = roundToNearestMultipleOfThree(80, 3)
print(nearestMultiple) --81
nearestMultiple = roundToNearestMultipleOfThree(97, 3)
print(nearestMultiple) --96
``````

My way because yes

``````function roundtosomething(numba,specificnumber)
local num = "0."
for i = 1,specificnumber do
num ..= 0
end
num ..= 5
return math.floor(numba+tonumber(num))
end
print(roundtosomething(4.005,2)) -- 4.01``````