Re: simple string format question

Discussion in 'Python' started by Chris Rebert, Oct 15, 2012.

  1. Chris Rebert

    Chris Rebert Guest

    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.


    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
    Chris Rebert, Oct 15, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ankur
    Replies:
    1
    Views:
    12,704
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  2. Neal Becker

    simple string format question

    Neal Becker, Oct 15, 2012, in forum: Python
    Replies:
    0
    Views:
    147
    Neal Becker
    Oct 15, 2012
  3. Chris Angelico
    Replies:
    3
    Views:
    144
    Mark Lawrence
    Mar 1, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    124
    Peter Otten
    Feb 28, 2013
  5. Rick Johnson
    Replies:
    0
    Views:
    132
    Rick Johnson
    Feb 28, 2013
Loading...

Share This Page