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


Groups > comp.lang.python > #102096

Re[2]: .format won't display my value with 2 decimal places: Why?

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re[2]: .format won't display my value with 2 decimal places: Why?
Date 2016-01-25 17:19 +0000
Message-ID <mailman.221.1453742395.15297.python-list@python.org> (permalink)

Show all headers | View raw



On 2016-01-25 16:51:36, "Ian Kelly" <ian.g.kelly@gmail.com> wrote:

>On Sun, Jan 24, 2016 at 2:20 PM, MRAB <python@mrabarnett.plus.com> 
>wrote:
>>  The format method, on the other hand, belongs to the format string 
>>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 
>>'format'
>>  method with 2 values for that string, the first being 4.0 (used) and 
>>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 
price is {0:.2f}'.

In general, the positional arguments can be used in any order, and there 
can also be keyword arguments, so it would need to remember which 
arguments had been used. Would it be worth it?

Do you really want to insist that the format string always used _all_ of 
the arguments?

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


Thread

Re[2]: .format won't display my value with 2 decimal places: Why? MRAB <python@mrabarnett.plus.com> - 2016-01-25 17:19 +0000

csiph-web