simple string format question

Kamlesh Mutha mkamlesh at gmail.com
Mon Oct 15 08:50:40 EDT 2012


There doesn't seem to be any direct way to achieve this.

Maybe you can do something like this:

import math

x = 3.05

if math.modf(x)[0] != 0.0: print x


Cheers,
-Kamlesh




On Mon, Oct 15, 2012 at 5:59 PM, Chris Rebert <clp2 at rebertia.com> wrote:

> On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker <ndbecker2 at gmail.com> 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.
>
>
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Faith waiting in the heart of a seed promises a miracle of life which it
can not prove!
-Ravindranath Tagore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121015/d904d5ca/attachment.html>


More information about the Python-list mailing list