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

How do you round numbers to nearest whole number in lua?

hi when i try to get a position of something it just has a bunch of decimals behind it is there a way to round this so it doesnt have these decimals?

2 comments

0

math.floor rounds down, math.ceil rounds up. You could build a custom function for this.

0

math.round

1 answer

you can use math.floor or math.ceil

math.floor will round a number down math.ceil will round a number up

Examples:

print(math.floor(8.793186816)) --> 8
print(math.ceil(8.4391595)) --> 9

So what we do to round it up to the nearest whole number is we add 0.5 to it(if your'e using math.floor) but if you're using math.ceil then subtract 0.5

print(math.floor(8.793186816 + 0.5)) --> 9
print(math.ceil(8.4391595 - 0.5)) --> 8

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