lua-users home
lua-l archive

Re: math.floor and rounding

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


James Graves <ansible <at> xnet.com> writes:
> If you want to _round_ off a floating point number, instead of
> flooring it, here is the rounding function I have in my standard
> library:
> 
> function round(num) return math.floor(num+.5) end
:::
> Are you handling negative numbers? math.floor may not be what you want
> in that case:
This is my way to get symmetric behavior
local function round(x)
	if x<0 then return -round(-x) end
	return math.floor(x+0.5)
end
Test with -1.5 : my round=-2 your round=-1
Regards Jørgen

AltStyle によって変換されたページ (->オリジナル) /