Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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?

0
math.floor rounds down, math.ceil rounds up. You could build a custom function for this. greatneil80 2647 — 3y
0
math.round RealTinCan 217 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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
Ad

Answer this question