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: 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 Subject: Re: Cheat sheet for the new string formatting? Date: Mon, 08 Jun 2015 23:48:25 +0300 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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.'