Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102097
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: .format won't display my value with 2 decimal places: Why? |
| Date | Tue, 26 Jan 2016 04:23:32 +1100 |
| Lines | 48 |
| Message-ID | <mailman.222.1453742620.15297.python-list@python.org> (permalink) |
| References | <56A1B69F.3040300@gmail.com> <em59031268-5aa7-4141-b0ba-f5de9a9f9261@andromeda> <CALwzidmWXJdFq1DbW3_zTGVeDbxW0W2BeLcyc20PXCAiGFr-1A@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de fpEdbJ7+q2AAyt9pH3rkFw+6ljLfAQZGIKSfs2FB1z1w== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'compiler': 0.05; 'error:': 0.05; 'mrab': 0.05; 'formatting': 0.07; 'method,': 0.07; 'cc:addr:python-list': 0.09; '1);': 0.09; 'ignoring': 0.09; 'positional': 0.09; 'subject:Why': 0.09; 'translations': 0.09; 'example:': 0.10; 'jan': 0.11; 'exception': 0.13; 'question.': 0.13; '"2"': 0.16; '2);': 0.16; '2016': 0.16; '24,': 0.16; '3:51': 0.16; 'arguments;': 0.16; 'cc:name:python': 0.16; 'fatal': 0.16; 'formatting,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parameters,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reorder': 0.16; 'result:': 0.16; '{0}': 0.16; 'wrote:': 0.16; 'string': 0.17; "shouldn't": 0.18; 'skip:{ 20': 0.18; 'string,': 0.18; 'hack': 0.18; 'all,': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '4.0': 0.22; 'arguments': 0.22; 'explicit': 0.22; 'am,': 0.23; "python's": 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'error': 0.27; 'equivalent': 0.27; 'parameters': 0.27; 'used,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'values': 0.28; 'raise': 0.29; 'probably': 0.31; 'skip:. 10': 0.32; 'useful': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'received:209.85': 0.36; '(3)': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; '(2)': 0.37; 'things': 0.38; 'doing': 0.38; 'received:209': 0.38; '(1)': 0.38; 'why': 0.39; 'format': 0.39; 'does': 0.39; 'subject:with': 0.40; 'some': 0.40; 'percent': 0.66; 'worth': 0.67; 'price': 0.69; '26,': 0.72; 'special': 0.73; 'chrisa': 0.84; 'pike': 0.84; 'subject:value': 0.84; 'to:none': 0.91; 'hassle': 0.91; 'hand,': 0.97 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=idn4Y1BTz1jvwrby8fj5WLF4qRb/T2KK+4NUgYlTvJs=; b=Vq2839p4GyrfQKxvPIw9YJ8po0sLofF8qRJpWuTA/q1nBf2hg5vS5NuGWnxaOWAbNB 6Ho0tTCodGEKNciSgwl/NZVddNLSM9UflbMNx7en7GhH4p6HVH5Cj438V6n3RGHvVbVP crzYLo/seasfAnpF/UcD/EQiROet1XUaxwQk8wbHHIbns+4t9WKylVYC6Vj8862fMpH2 g5Z9KaKq37HQIOg6hTfj9UHSauICP1QgYxAMfnyUxqZCRwvXwQlztVW11HbaJrZn7rzc E72szSJTHrSme1SMGv7DY4NwkC4wu8z8IQsEi/C5EP9QmnyYGsau2XOsmvya3x9EoiHg yr5w== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=idn4Y1BTz1jvwrby8fj5WLF4qRb/T2KK+4NUgYlTvJs=; b=WgWdYuLI319Dw06CS0TInvI4G2m+rOTZmGCnnV0+6pl5on7tYDGg7QWZ+1yhz7OPjm WQabXOZHZTa5V71PHgU3BwIxbKyCrPSx/MTslGIedmtUDXjlRrvj02kUSlupCa/mYS88 zhXBB4/hxWVQ2sPeUfig5BvGF3ZRhEt3UFotealzG0WuEJXWVTHDnfhTlhZbylj3Ulqk w8+ZEIICcZK9oiq5nL5KN/thV+TdVNPqaSMxgf/yqGtoWDv8NTFJCQcvjBBdIiJcSVcB R52VOo73oX3G94hysv39RuHLHEA1zg5a5STdB2VSjsnTj/GxBq/APk8fs9pKPT8++B4V cJLA== |
| X-Gm-Message-State | AG10YOQAnNPV3dSD81mNJbe/SEHpVgu51E2RX+P2TguOCH9hz2kXXl2N8YFNv9UupPVuDdty/Q19ASvr9fd1+g== |
| X-Received | by 10.107.40.76 with SMTP id o73mr16640515ioo.157.1453742612394; Mon, 25 Jan 2016 09:23:32 -0800 (PST) |
| In-Reply-To | <CALwzidmWXJdFq1DbW3_zTGVeDbxW0W2BeLcyc20PXCAiGFr-1A@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> |
| Xref | csiph.com comp.lang.python:102097 |
Show key headers only | View raw
On Tue, Jan 26, 2016 at 3:51 AM, 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's a very good question. I suspect the answer will have to do with
i18n and the way you can reorder arguments; if some translations don't
use a token at all, it shouldn't be a fatal error that you have to
hack around by formatting something into zero characters.
It may be worth adding a special case: if no positional selectors are
used, the number of arguments must match the number of placeholders.
For comparison, here's how Pike does things (%O is roughly equivalent
to Python's {!r}):
> sprintf("%O", 1);
(1) Result: "1"
> sprintf("%O %O", 1, 2);
(2) Result: "1 2"
> sprintf("%O", 1, 2);
Compiler Error: 1: Too many arguments to sprintf (expected 2 arguments).
Compiler Error: 1: Got : int(2..2).
> sprintf("%[0]O", 1, 2);
(3) Result: "1"
> sprintf("%[1]O", 1, 2);
(4) Result: "2"
If you're doing i18n, you probably want to use {0} {1} anyway; and if
you're deliberately ignoring some of the parameters, it's not too much
hassle to be explicit about which parameters you're not ignoring.
IMO this would be a useful protection. You get it with percent
formatting, but currently not with .format().
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: .format won't display my value with 2 decimal places: Why? Chris Angelico <rosuav@gmail.com> - 2016-01-26 04:23 +1100
csiph-web