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


Groups > comp.lang.python > #41927

Re: I need a neat way to print nothing or a number

From Peter Otten <__peter__@web.de>
Subject Re: I need a neat way to print nothing or a number
Date 2013-03-26 18:22 +0100
Organization None
References <jms82a-6sl.ln1@chris.zbmc.eu> <CAPTjJmp2-x0Nsw9JtLscF3QfHpYGb9fgCRAvFMf7EwVdWqt24g@mail.gmail.com> <loom.20130326T175824-258@post.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3759.1364318537.2939.python-list@python.org> (permalink)

Show all headers | View raw


Wolfgang Maier wrote:

> Chris Angelico <rosuav <at> 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 '').

Here's a round-about way:

class Prepare:
    def __init__(self, value):
        self.value = value
    def __format__(self, spec):
        if self.value is None or self.value == 0:
            return format(0.0, spec).replace(".", " ").replace("0", " ")
        elif isinstance(self.value, str):
            return self.value.rjust(len(format(0.0, spec)))
        return format(self.value, spec)

def prepare(row):
    return map(Prepare, row)

data = [
    ("Credit", "Debit", "Description"),
    (100, 0, "Initial balance"),
    (123.45, None, "Payment for cabbages"),
    (0.0, 202.0, "Telephone bill"),
]

for row in data:
    print("{:10.2f} {:10.2f} {}".format(*prepare(row)))


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I need a neat way to print nothing or a number cl@isbd.net - 2013-03-26 15:50 +0000
  Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 03:08 +1100
  Re: I need a neat way to print nothing or a number John Gordon <gordon@panix.com> - 2013-03-26 16:29 +0000
  Re: I need a neat way to print nothing or a number Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-26 17:06 +0000
  Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 04:14 +1100
  Re: I need a neat way to print nothing or a number Peter Otten <__peter__@web.de> - 2013-03-26 18:22 +0100
  Re: I need a neat way to print nothing or a number Ethan Furman <ethan@stoneleaf.us> - 2013-03-26 10:21 -0700
  Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 05:48 +1100
  Re: I need a neat way to print nothing or a number Tony the Tiger <tony@tiger.invalid> - 2013-03-27 01:11 -0500
  Re: I need a neat way to print nothing or a number Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-27 08:23 +0000

csiph-web