floating point rounding
greg
greg at cosc.canterbury.ac.nz
Sat Mar 10 04:33:36 EST 2007
John Henry wrote:
> Or more precisely:
>
> round(0.014999999999999999,2)
No, that *won't* solve the problem. Using a slightly
different example,
>>> x = 1.5 * 0.1
>>> x
0.15000000000000002
>>> round(x, 2)
0.14999999999999999
The problem is that floats are stored internally in
binary, not decimal, and numbers like 0.1 and 0.01
have no exact representation as a binary float.
Using round() doesn't help, because the result is
still a binary float, and the result you're after
still can't be represented.
The best you can do is to *display* it rounded
to the number of digits you want using formatting,
e.g.
>>> "%.2f" % x
'0.15'
Alternatively, use the Decimal module, which stores
numbers as decimal and does arithmetic in ways that
will match your calculator. It's slower, though.
--
Greg
More information about the Python-list
mailing list