in reply to Re: int($x) gives strange result
in thread int($x) gives strange result
$ perl -wle ' my $x = 123456.15; print +(int($x*100.1))/100;' 123579.6
So it turned 123456.15 into 123579.6 just to get rid of rounding errors - and produced errors which are about a thousand times larger.
Instead you can add (not multiply) a small constant before calling int:
$ $ perl -wle ' my $x = 1.15; print +(int($x*100 + 1e-8))/100;' 1.15
Depending on the range of the used numbers you'd have to think a bit more about the size of the small number you add.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: int($x) gives strange result
by SFLEX (Chaplain) on Aug 24, 2009 at 08:52 UTC |