How do I round a number to the nearest 0.1?

I have a range finder script, but it prints stuff like 10.502906799316406, which is very long. How do I round it to the nearest tenth or hundrenth?

Here’s one way.
Multiply by the reciprocal of the place value you want (i.e. 10 if you’re rounding to the tenth)
Use math.round on the number.
Then, divide the result by the reciprocal you first multiplied by.
Example: 0.178 rounded to the nearest tenth: 0.178 → 1.78 → 2 → 0.2

2 Likes

You can use string formatting, example (note this isn’t rounding, it just cuts the decimals):

local num = 10.502906799316406
local formatted = string.format(tostring(num), "%.2f")

print(formatted)

I also found this for rounding:

local value = 0.984375
value *= 100
value = math.floor(value)
value = value / 100
3 Likes