Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92348

Re: Cheat sheet for the new string formatting?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'incorrect': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'valueerror': 0.09; 'output': 0.15; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.16; 'skip': 0.18; '>>>': 0.20; 'skip:" 30': 0.20; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'equivalent': 0.27; 'decimal': 0.29; 'code': 0.31; 'subject:?': 0.34; 'skip:- 10': 0.34; 'to:addr :python-list': 0.35; 'unknown': 0.35; 'display': 0.37; 'subject:: ': 0.37; 'received:org': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'subject:the': 0.40; 'why': 0.40; 'charset:windows-1252': 0.65
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Serhiy Storchaka <storchaka@gmail.com>
Subject Re: Cheat sheet for the new string formatting?
Date Mon, 08 Jun 2015 23:48:25 +0300
References <CANc-5Uyv8Ma3G-JfRh7PT-yf4QzJaWnqgyYN9c8=mrnGx_5yRQ@mail.gmail.com> <CANc-5UxhxqE2P_U11Ut98Re6Eq+siXTXcrbtG+w09yX9UpvkRw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 193.202.118.163
User-Agent Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
In-Reply-To <CANc-5UxhxqE2P_U11Ut98Re6Eq+siXTXcrbtG+w09yX9UpvkRw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.303.1433796531.13271.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433796531 news.xs4all.nl 2895 [2001:888:2000:d::a6]:48528
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92348

Show key headers only | View raw


On 08.06.15 23:32, Skip Montanaro wrote:
> This is counterintuitive:
>
>  >>> "{:.3}".format(-0.00666762259822)
> '-0.00667'
>  >>> "{:.3f}".format(-0.00666762259822)
> '-0.007'
>  >>> "%.3f" % -0.00666762259822
> '-0.007'
>  >>> "{:.3s}".format(-0.00666762259822)
> ValueError Unknown format code 's' for object of type 'float'
>
> Why does the first form display five digits after the decimal point? Why
> don't floats support "{:.Ns}"? (I know I can use "{!s}".)

"{:.3}" for floats is equivalent to "{:.3g}".

 >>> "{:.3g}".format(-0.00666762259822)
'-0.00667'
 >>> "%.3g" % -0.00666762259822
'-0.00667'

Format code 's' would produce incorrect and meaningless output for floats.

 >>> "{!s:.3}".format(-0.00666762259822)
'-0.'
 >>> "%.3s" % -0.00666762259822
'-0.'
 >>> "{!s:.3}".format(-0.0000666762259822)
'-6.'
 >>> "%.3s" % -0.0000666762259822
'-6.'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Cheat sheet for the new string formatting? Serhiy Storchaka <storchaka@gmail.com> - 2015-06-08 23:48 +0300

csiph-web