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


Groups > comp.lang.python > #102095 > unrolled thread

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

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2016-01-25 09:51 -0700
Last post2016-01-25 09:51 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: .format won't display my value with 2 decimal places: Why? Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-25 09:51 -0700

#102095 — Re: .format won't display my value with 2 decimal places: Why?

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-01-25 09:51 -0700
SubjectRe: .format won't display my value with 2 decimal places: Why?
Message-ID<mailman.220.1453740738.15297.python-list@python.org>
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?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web