[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: math.floor and rounding
- From: Tony Finch <dot@...>
- Date: Mon, 20 Sep 2010 16:45:09 +0100
On Mon, 20 Sep 2010, Jose wrote:
>
> I want to round numbers with two decimal digits. I use math.floor( n *
> 100 ) but sometimes I have situations like:
> n = 10.20; math.floor( n * 100 ) --> 1019
> My current solution is to always add 0.001 to n, but is this the right
> approach ?
You should add 0.005 to get (roughly) round-to-nearest.
Lua is a little bit weak in this area because C89 doesn't have a
round-to-nearest function (only floor and ceil). (C99 is more complete.)
Tony.
--
f.anthony.n.finch <dot@dotat.at> http://dotat.at/
HUMBER THAMES DOVER WIGHT PORTLAND: NORTH BACKING WEST OR NORTHWEST, 5 TO 7,
DECREASING 4 OR 5, OCCASIONALLY 6 LATER IN HUMBER AND THAMES. MODERATE OR
ROUGH. RAIN THEN FAIR. GOOD.