this script can sometimes output some number with a massive amount of decimals and I can’t think of a way to solve this.

What solutions have you tried so far? I tried looking for solutions on the devforum but all I could find was for rounding to the nearest whole number and shortening numbers (example 2k)

I believe what you could do here is divide your number by 100, as to make it a 2 point decimal, use either your math.floor() or math.ceil() rounding option and then times it by 100 again. This way, you can round it as a decimal and then get your original number back.

I tested it in studio and it seemed to work, so your code would look something like this.

For anyone confused how math.floor(x * 10^n) / (10^n) actually works here is an explanation.

The x * 10^n is meant to shift x by n places. So if you have the number 10.25 and multiply by 10^2 which is 100 that leaves you with 1025. Given this logic you could now floor the number which will cut off the remaining decimal portion if there was any and then divide by the original 10^n because you want to put your number back to the same significant figures.

local roundDecimals = function(num, places) --num is your number or value and places is number of decimal places, in your case you would need 2
places = math.pow(10, places or 0)
num = num * places
if num >= 0 then
num = math.floor(num + 0.5)
else
num = math.ceil(num - 0.5)
end
return num / places
end
print(roundDecimals(142.165, 2)) --> 142.17

Doesn’t seem to round them properly all the time from what I’ve tested though, I tried printing out the same number (14.4215) using both methods and got out 14.421 with this method above and 14.422 with the one I posted. But I guess that doesn’t really matter.

Cheat code:
Use the %d pattern instead to remove the padding!

It does not do rounding though, it just removes the decimal portion. To do rounding add .5 to x;

("%d"):format(x + .5)

(For anyone wondering this works because if the first decimal place is greater than .5 that means you round up, and adding .5 effectively rounds it up and on the floor portion of the math (%d pattern) the decimal portion gets removed)