Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re[2]: .format won't display my value with 2 decimal places: Why? Date: Mon, 25 Jan 2016 17:19:46 +0000 Lines: 38 Message-ID: Reply-To: MRAB Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de Z4u0XjU18ogQomOKpzBwygpyOtBx2e+65V83u+vovocA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'used.': 0.05; 'method,': 0.07; 'positional': 0.09; 'subject:Why': 0.09; 'example:': 0.10; 'jan': 0.11; 'exception': 0.13; '2016': 0.16; '24,': 0.16; '>on': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string': 0.17; 'skip:{ 20': 0.18; 'string,': 0.18; '4.0': 0.22; 'arguments': 0.22; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'equivalent': 0.27; 'values': 0.28; 'arguments,': 0.29; 'raise': 0.29; 'skip:. 10': 0.32; 'there': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'method': 0.37; 'subject:[': 0.39; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'header:Message-Id:1': 0.61; 'header:Reply- To:1': 0.67; 'worth': 0.67; 'price': 0.69; 'subject:value': 0.84; 'to:name:python': 0.84; 'hand,': 0.97 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=9hWY2m1uP5lx4YsNEgAA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 In-Reply-To: User-Agent: eM_Client/6.0.24432.0 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: , Xref: csiph.com comp.lang.python:102096 On 2016-01-25 16:51:36, "Ian Kelly" wrote: >On Sun, Jan 24, 2016 at 2:20 PM, MRAB =20 >wrote: >> The format method, on the other hand, belongs to the format string=20 >>it's >> attached to. In this example: >> >> 'The new price is {}' .format(newPrice, '.2f') >> >> the format string is 'The new price is {}' and you're calling its=20 >>'format' >> method with 2 values for that string, the first being 4.0 (used) and= =20 >>the >> second on being '.2f' (unused). >> >> What you want is: >> >> print('The new price is {:.2f}'.format(newPrice)) > >Why doesn't str.format raise an exception when passed extra positional >arguments? > That format string uses auto-numbering, and it's equivalent to 'The new=20 price is {0:.2f}'. In general, the positional arguments can be used in any order, and there= =20 can also be keyword arguments, so it would need to remember which=20 arguments had been used. Would it be worth it? Do you really want to insist that the format string always used _all_ of= =20 the arguments?