I probably overdid it, made this in like 5 minutes:
local function RoundDepth(num,depth)
return tonumber(string.sub(num,1,string.find(num,'%.'))..math.ceil(tonumber(string.sub(num,string.find(num,'%.')+1,string.find(num,'%.')+depth))-.5))
end
print(RoundDepth(811324234.499429924,4))
To understand more,
local function RoundDepth(num,depth)
local n = tostring(num)
local x = math.ceil(tonumber(string.sub(n,string.find(n,'%.')+1,string.find(n,'%.')+depth))-.5)
return tonumber(string.sub(num,1,string.find(n,'%.'))..x)
end
print(RoundDepth(811324234.499429924,1))
You can replace the ceil with round, I just like using ceil apparently.
This basically turns the number into a string, gets the ".", gets the numbers after it, depending on how deep it is, suppose it was 2, 49 would round to 50 in 811324234.499429924.
x is basically me rounding the end of the number after the decimals by the depth
the last part after return is me joining the original number (without the decimals) along with the new rounded number.
If you have questions let me know.
Here is the link to the interpreter with the code:
runnable code
Edit: If the code in the first part is cut off, click view raw to see it whole
2 comments
you can use string.format to accomplish it.
check @greatneil80's answer