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

How do you round a decimal value to the nearest whole number?

It's a pretty simple question that doesn't require much explanation but this problem I'm having is annoying. So how do you change a decimal number like 123.343 and round it to the nearest whole number? I'm not the best at math. Any help?

2 answers

✓ Accepted answer

An elegant solution would be to add 0.5 and round it down.

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

There are several ways of rounding negative numbers. See this page for some more info. This implementation rounds -1.5 to -1, not -2.

1 comment

1

Thanks for the help!

print(math.floor(123.343))

math.floor(NUMBER HERE) allows the decimal to go to the nearest whole number.

1 comment

1

The nearest whole number to 0.9 is 1, not 0.

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