diff options
author | Stéphane Wirtel <stephane@wirtel.be> | 2019-02-19 09:26:02 (GMT) |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-02-19 09:26:02 (GMT) |
commit | c611db4942a07c81f54e6584615bbddc51034a77 (patch) | |
tree | 336998c386131b6187a77ffda7cb5163f9cbd8e1 /Doc | |
parent | d5409eb6c26c6bca2686762ce0fd5223bb845e8a (diff) | |
download | cpython-c611db4942a07c81f54e6584615bbddc51034a77.zip cpython-c611db4942a07c81f54e6584615bbddc51034a77.tar.gz cpython-c611db4942a07c81f54e6584615bbddc51034a77.tar.bz2 |
[2.7] bpo-35126: Fix a mistake in FAQ about converting number to string (GH-11911)
https://bugs.python.org/issue35126
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/faq/programming.rst | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 9190e1a..62b34b6 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -997,9 +997,27 @@ To convert, e.g., the number 144 to the string '144', use the built-in type constructor :func:`str`. If you want a hexadecimal or octal representation, use the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see the :ref:`formatstrings` section, e.g. ``"{:04d}".format(144)`` yields -``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``. You may also use -:ref:`the % operator <string-formatting>` on strings. See the library reference -manual for details. +``'0144'`` and ``"{:.3f}".format(1.0/3.0)`` yields ``'0.333'``. In Python 2, the +division (/) operator returns the floor of the mathematical result of division +if the arguments are ints or longs, but it returns a reasonable approximation of +the division result if the arguments are floats or complex:: + + >>> print('{:.3f}'.format(1/3)) + 0.000 + >>> print('{:.3f}'.format(1.0/3)) + 0.333 + +In Python 3, the default behaviour of the division operator (see :pep:`238`) has +been changed but you can have the same behaviour in Python 2 if you import +``division`` from :mod:`__future__`:: + + >>> from __future__ import division + >>> print('{:.3f}'.format(1/3)) + 0.333 + + +You may also use :ref:`the % operator <string-formatting>` on strings. See the +library reference manual for details. How do I modify a string in place? |