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

How to correctly round to the nearest five?

okay so i have been trying to make a block place script but i need to round to the nearest five for it to work here is my current script:

function round(x)
    return(math.floor(math.floor(x + 0.5)/5)*5)
end

it doesent work propely, for example if i run this script:

function round(x)
	return(math.floor(math.floor(x + 0.5)/5)*5)
end
for i = 0,25 do print(round(i)) end

if outputs:

0 (x5)
5 (x5)
10 (x5)
15 (x5)
20 (x5)
25

it outputs because it doesent round to the nearest five it rounds to the five under it, does anyone know any way to fix any help is appreciated

3 comments

0

Do this: "if num % 5 == 0 then".

0

Or use math.ceil().

0

what do you mean by that?

1 answer

found a solution even if it isnt very effective:

function round(n)
    return (math.floor((n/5) + 0.5))*5
end

if you know a better way please reply

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