When doing “correct mathematics” floating point effects cause a number 0.1
to become 0.100000000000001
.
So I want to do some “rounding”, but I’m unsure how to do it:
When doing something like sprintf('%.3f', $val)
there will be always three fractional digits like 0.100
.
For the concrete example I could use format %.1f
, but that 0.1
is just one example (it could be 0.125
as well).
What I want is “at most three fractional digits” (considered to be “enough” precision for my case).
Are there better solutions than print int($x * 1000) / 1000
while only using Perl core packages?
DB<1> $x=0.100000000000001
DB<2> print int($x * 1000) / 1000
0.1
DB<3>