Converting a float to a formatted outside of print command

Discussion in 'Python' started by stephen_b, Nov 23, 2009.

  1. stephen_b

    stephen_b Guest

    I'd like to convert a list of floats to formatted strings. The
    following example raises a TypeError:

    y = 0.5
    x = '.1f' % y
    stephen_b, Nov 23, 2009
    #1
    1. Advertising

  2. stephen_b

    Dan Bishop Guest

    On Nov 23, 3:15 pm, stephen_b <>
    wrote:
    > I'd like to convert a list of floats to formatted strings. The
    > following example raises a TypeError:
    >
    > y = 0.5
    > x = '.1f' % y


    You meant:

    x = '%.1f' % y
    Dan Bishop, Nov 23, 2009
    #2
    1. Advertising

  3. On Nov 23, 2009, at 4:15 PM, stephen_b wrote:

    > I'd like to convert a list of floats to formatted strings. The
    > following example raises a TypeError:
    >
    > y = 0.5
    > x = '.1f' % y




    You're missing a percent sign:

    x = '%.1f' % y

    or:

    print '%.1f' % 0.5


    Hope this helps
    Philip
    Philip Semanchuk, Nov 23, 2009
    #3
  4. stephen_b

    stephen_b Guest

    On Nov 23, 3:17 pm, Dan Bishop <> wrote:
    > You meant:
    >
    > x = '%.1f' % y


    Thanks, I'm a dufus today.
    stephen_b, Nov 23, 2009
    #4
  5. Vlastimil Brom, Nov 23, 2009
    #5
    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. bd
    Replies:
    0
    Views:
    605
  2. mathieu
    Replies:
    9
    Views:
    730
    James Kanze
    Sep 15, 2007
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,505
    James Kanze
    Oct 8, 2009
  4. migurus
    Replies:
    1
    Views:
    1,420
    Paul N
    Feb 19, 2010
  5. Dipesh Batheja
    Replies:
    1
    Views:
    557
    Bernard Kenik
    Nov 16, 2006
Loading...

Share This Page