[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Rounding a number to integer
- From: Bodolai Tibor <btb@...>
- Date: Fri, 29 Jul 2016 19:06:00 +0200
My solution is:
function math.roundf(value, factor)
local r
r = value / factor
if value < 0 then
r = math.ceil(r - 0.5)
else
r = math.floor(r + 0.5)
end
return r * factor
end
function math.round(value, decimals)
return math.roundf(value, 10^(-decimals))
end
2016.07.29. 10:06 keltez¨¦ssel, ºêØ ¨ªrta:
> Lua 5.3.0 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>> 100000*1.005
> 100500.0
>> math.floor(100000*1.005)
> 100499
>
> I want a function like math.round.
>
>> math.round(100000*1.005)
> 100500
>
> Have some good idea?