This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: float->str rounding bug
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mrnolta, tim.peters
Priority: normal Keywords:

Created on 2006-05-01 14:37 by mrnolta, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg28396 - (view) Author: Michael Nolta (mrnolta) Date: 2006-05-01 14:37
Hi,

Here's my problem:

  >>> print "%.2f" % 2.195
  2.19

The output should be "2.20".

It's sensitive to whether the value is greater than 1.
For example, this works:

  >>> print "%.4f" % 2.195e-2
  0.0220

Here's my full version info:

  Python 2.4.1 (#1, May 16 2005, 15:19:29)
  [GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2

-Mike
msg28397 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-05-01 15:54
Logged In: YES 
user_id=31435

This isn't a bug.  You should read the Python Tutorial's
Appendix on floating-point issues.  As you can see from

>>> 2.195
2.1949999999999998

the closest representable float is actually a little bit
smaller than the decimal 2.195, so there's no reason for it
to "round up".  Try the same thing in C (or any other
language exposing your hardware's binary floating point),
and you should see the same result.

If you need exact decimal values, use Python's "decimal"
module instead.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43305
2006-05-01 14:37:31mrnoltacreate