Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; 'float': 0.07; 'expression:': 0.09; 'false.': 0.09; 'formatting': 0.09; 'plug': 0.09; 'subject:number': 0.09; 'evaluates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'precedence': 0.16; 'ternary': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'subject:need': 0.19; 'fit': 0.20; '>>>': 0.22; 'logical': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'gives': 0.31; 'code': 0.31; 'apparently': 0.31; 'operators': 0.31; 'table,': 0.31; 'writes:': 0.31; 'there.': 0.32; "can't": 0.35; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'received:209': 0.37; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'according': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'easy': 0.60; 'expression': 0.60; 'tell': 0.60; 'first': 0.61; 'mar': 0.68; 'ethan': 0.84; 'furman': 0.84; 'subject:nothing': 0.84; 'working,': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=xdy2h3/7pMn8NwyZPKO7/hqzODHM+HuWqMulLFXjSlk=; b=ph27yPyXp82pNzSIELYQhfAiQQk9TAJj4DDYqpknFCF4zRPjrj57fJLjj3eVW5HNGP WhU2uEJGKax6IeEoKpWltoUdh4iZqc8x+ZGK0pMlBZT2Jwz78/54fR38WWB9FOX2ZTlx eZqEleElUzVQa6Yl5HWenQYaxoaCM1xTMjwGcSZhUDh9lz5aLZp6Dce5W164/smsXQwl Y9oCe/ghBy5U01FNI6QnDAGzKT4sJwnCVswwU1bzV6DdpyJ+qkXgHudMOOTvaV4o8EA2 Ul9LEnr6tCwoSzR7sy36mU5gPqrE89hr0r5t9H4Kw0ZZkvcpxTQstjbxIveAqp5xDplU 8p7Q== MIME-Version: 1.0 X-Received: by 10.58.56.161 with SMTP id b1mr20501472veq.42.1364323719954; Tue, 26 Mar 2013 11:48:39 -0700 (PDT) In-Reply-To: <5151D91F.8080506@stoneleaf.us> References: <5151D91F.8080506@stoneleaf.us> Date: Wed, 27 Mar 2013 05:48:39 +1100 Subject: Re: I need a neat way to print nothing or a number From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364323722 news.xs4all.nl 6868 [2001:888:2000:d::a6]:57327 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41940 On Wed, Mar 27, 2013 at 4:21 AM, Ethan Furman wrote: > On 03/26/2013 10:06 AM, Wolfgang Maier wrote: >> >> Chris Angelico gmail.com> writes: >> >>> >>> Try printing out this expression: >>> >>> "%.2f"%value if value else '' >>> >>> Without the rest of your code I can't tell you how to plug that in, >>> but a ternary expression is a good fit here. >>> >>> ChrisA >>> >> >> Unfortunately, that's not working, but gives a TypeError: a float is >> required >> when the first value evaluates to False. >> Apparently it's not that easy to combine number formatting with logical >> operators - the same happens with my idea ('{:.2f}').format(value or ''). > > > Use parens then: > > ("%.2f" % value) if value else '' According to the operator precedence table, the parens are unnecessary there. ChrisA