lua-users home
lua-l archive

Re: math.floor and rounding

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


On 9/20/2010 11:32 PM, 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 ?
If it's *precisely* 2 decimal places, consider instead to do your calculations without any fractions, i.e. use 1020 etc. Look at it this way -- if you're doing things like that, perhaps you are implementing things wrongly. Floating point can only have fractions that are the exact sums of 1/2, 1/4, 1/8, etc. If you expect *exact* values for things like 10.20, then you're doing it wrong. And if you're doing currency calculations like that, please smash your keyboard over your head ;-)
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

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