Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; 'e.g.,': 0.07; 'subject:question': 0.08; 'django,': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.10; 'template': 0.11; '2.00': 0.16; 'columns': 0.16; 'oct': 0.16; 'subject:simple': 0.16; 'temp': 0.16; 'url:djangoproject': 0.16; 'mon,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'specify': 0.17; 'url:dev': 0.17; 'written': 0.20; 'finally,': 0.22; 'cheers,': 0.23; '15,': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'cc:addr:gmail.com': 0.27; 'format,': 0.27; 'actual': 0.28; 'chris': 0.28; 'no,': 0.29; 'probably': 0.29; 'up.': 0.31; 'point': 0.31; 'implement': 0.32; 'project': 0.34; 'there': 0.35; 'add': 0.36; 'created': 0.36; 'but': 0.36; 'should': 0.36; 'uses': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'url:en': 0.38; 'received:192': 0.39; 'space': 0.39; 'received:192.168': 0.40; 'your': 0.60; "you'll": 0.62; 'more': 0.63; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'yourself,': 0.75; 'find.': 0.84; 'url:templates': 0.91; 'to:none': 0.93 Date: Mon, 15 Oct 2012 09:02:58 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 Subject: Re: simple string format question References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:qWVy+4uoCcWK85cTHnKtn+cA27oynLI6V6tHGUXt18p QnX8Qy3PQMu1pD/XrryelO45KuMTaXsMcPW0iQwYl0Pg4nPAPR 0IaugLra3FsfMleoeKrE3axztY0DDU1NUBihIWmOMLadJ5923L w5t+PDcyiIjZt+1DeQH3SsK+nXZUiC+Z4dIOH9b2b3ag/e5qqc C9ZXvWkyx2oFfqpGohZaGk2GKcMUIzMFWnCs1KNqyKhXgpcXe3 tLKxAPGAjHfEi/01CZOZTIe5pJ5GaNe9XLZfBxVQwVVk1dA1Lw uzrnI/WoOCNvU2KgILwbTeIVpvuopFRJ9PUWVI0xmWrC0irqw= = Cc: Neal Becker , python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350306208 news.xs4all.nl 6854 [2001:888:2000:d::a6]:57417 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31296 On 10/15/2012 08:29 AM, Chris Rebert wrote: > On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker wrote: >> Is there a way to specify to format I want a floating point written with no more >> than e.g., 2 digits after the decimal? I tried {:.2f}, but then I get all >> floats written with 2 digits, even if they are 0: >> >> 2.35 << yes, that's what I want >> 2.00 << no, I want just 2 or 2. > Not that I can find. Seems you'll have to implement it yourself. To do it yourself, probably best to use a temp string value, created by format "{: f}". (Notice the space before the f, to reserve space for the sign) Then, slice that value to length 4 . Finally, in your actual format, use :5s for a format. This should add the blanks for padding, so other columns still line up. > > In the event that your project uses Django, there happens to be a > template tag for this (pass it -2 in your case): > https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#floatformat > > Cheers, > Chris -- DaveA