[issue23627] Incorrect string formatting of float values in Python 2.7

Eric V. Smith report at bugs.python.org
Tue Mar 10 16:01:09 CET 2015


Eric V. Smith added the comment:

By using %s, you're asking the formatting code to first convert the parameter to a string. Then, by using .10, you're asking it to truncate the value.

It's essentially equivalent to:

>>> str(-7.7176718e-05)
'-7.7176718e-05'
>>> str(-7.7176718e-05)[:10]
'-7.7176718'

and:

>>> str(-0.0000771767)
'-7.71767e-05'
>>> str(-0.0000771767)[:10]
'-7.71767e-'

This isn't a bug. If you want more control over the formatting, use %f, %e, or %g:

>>> '%.10f' % -7.7176718e-05
'-0.0000771767'

----------
nosy: +eric.smith
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23627>
_______________________________________


More information about the Python-bugs-list mailing list