Issue #9337: Make float.__str__ identical to float.__repr__.

(And similarly for complex numbers.)
This commit is contained in:
Mark Dickinson 2010-08-04 20:56:28 +00:00
parent b6c5074920
commit 388122d43b
10 changed files with 63 additions and 85 deletions

View file

@ -92,18 +92,17 @@ thing in all languages that support your hardware's floating-point arithmetic
(although some languages may not *display* the difference by default, or in all
output modes).
Python's built-in :func:`str` function produces only 12 significant digits, and
you may wish to use that instead. It's unusual for ``eval(str(x))`` to
reproduce *x*, but the output may be more pleasant to look at::
For more pleasant output, you may may wish to use string formatting to produce a limited number of significant digits::
>>> str(math.pi)
>>> format(math.pi, '.12g') # give 12 significant digits
'3.14159265359'
>>> format(math.pi, '.2f') # give 2 digits after the point
'3.14'
>>> repr(math.pi)
'3.141592653589793'
>>> format(math.pi, '.2f')
'3.14'
It's important to realize that this is, in a real sense, an illusion: you're
simply rounding the *display* of the true machine value.