Better round Function

its in the title but I need a round function that rounds. The old one I used just removed the decimal, which was not ideal so if anyone can help that will be Awesome :smiley:

Example:

local x = 1.67
local y = 1.36

round(x)
round(y)

output/returned value:

2
1

The first example would just be math.ceil, the second one would be math.floor. But if you’re looking for rounding to the nearest integer then just use:

local function round(n)
    return math.floor(n + 0.5)
end

print(round(1.67)) -- 2
print(round(1.36)) -- 1
function Round(Number: number, Precision: number?) : number
	local Places = (Precision) and (10^Precision) or 1
	return (((Number * Places) + 0.5 - ((Number * Places) + 0.5) % 1)/Places)
end

--Round(input, decimal places)

Round(1.23456, 2) -- Prints 1.23
Round(1.23456) -- Prints 1

Bit more powerful round function I use, @sjr04 's solution would be a more simplified implementation

2 Likes

I would rather use yours since I need perfect precision :smiley: