Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'float': 0.07; 'expression:': 0.09; 'false.': 0.09; 'formatting': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'plug': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'subject:number': 0.09; '~ethan~': 0.09; 'evaluates': 0.16; 'received:69.93': 0.16; 'ternary': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'subject:need': 0.19; 'fit': 0.20; 'header:User-Agent:1': 0.23; 'logical': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'skip:( 20': 0.30; 'gives': 0.31; 'code': 0.31; 'apparently': 0.31; 'operators': 0.31; 'writes:': 0.31; "can't": 0.35; 'but': 0.35; 'charset:us- ascii': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'easy': 0.60; 'expression': 0.60; 'tell': 0.60; 'received:173': 0.61; 'first': 0.61; 'subject:nothing': 0.84; 'working,': 0.84 Date: Tue, 26 Mar 2013 10:21:35 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: I need a neat way to print nothing or a number References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.235]) [173.12.184.235]:53043 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364318846 news.xs4all.nl 6887 [2001:888:2000:d::a6]:41654 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41931 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 '' -- ~Ethan~