All pastes #72498 Raw Edit

Something

public text v1 · immutable
#72498 ·published 2006-06-26 22:18 UTC
rendered paste body
--[[
Luadoc doesn't support examples or remarks, so I added them hackishly by appending html to the return tag.

Advantage: No need to tinker with luadoc itself, and it's portuguese comments
Disadvantage: People reading the source will claw their eyes out
--]]

-- Round a number to the nearest whole number or to a specified number of decimal points.
-- By Kaelten and Neronix.

-- @param num A number.
-- @param decimalpoints (optional) If present, the number of decimal points to round to.

-- @return Rounded number.<br><i>Remarks</i>: If you're rounding to the nearest whole, don't bother passing decimalpoints. It'll run 5x slower if you do decimalpoints = 0.<br><i>Examples</i>:<p>utilsmaths:Round(32.33) <small>Returns 32</small><br>utilsmaths:Round(32.8) <small>Returns 33</small><br>utilsmaths:Round(32.5) <small>Returns 33</small><br>utilsmaths:Round(32.394, 2) <small>Returns 32.39</small><br>utilsmaths:Round(456, -1) <small>Returns 450. Funky!</small></p>.

function AceUtilsMaths:Round(num, decimalpoints)
	if not decimalpoints then
		-- Round to nearest whole number
		return math.floor(num + 0.5)
	else
		return math.floor(num * (10^decimalpoints)) / (10^decimalpoints)
	end
		
	-- I know I could have done decimalpoints = 0 to round to the nearest whole number, but math.floor(num + 0.5) is 5x faster 
end