Formatting floats

Discussion in 'Python' started by Kevin Smith, Dec 11, 2003.

  1. Kevin Smith

    Kevin Smith Guest

    I'm trying to get floats formatted in exponent notation but with the
    first non-zero number to the right of the decimal instead of to the left (
    e.g. '-.107E2' instead of '-1.07E1'). Is this possible using the string
    formatting operations?

    --
    Kevin Smith
    Kevin Smith, Dec 11, 2003
    #1
    1. Advertising

  2. Kevin Smith

    Dan Bishop Guest

    Kevin Smith <> wrote in message news:<>...
    > I'm trying to get floats formatted in exponent notation but with the
    > first non-zero number to the right of the decimal instead of to the left (
    > e.g. '-.107E2' instead of '-1.07E1'). Is this possible using the string
    > formatting operations?


    >>> def formatFloat(x):

    .... mantissa, exponent = ('%e' % x).split('e')
    .... mantissa = float(mantissa) / 10
    .... exponent = int(exponent) + 1
    .... return '%fe%d' % (mantissa, exponent)
    ....
    >>> formatFloat(-1.07E1)

    '-0.107000e2'
    Dan Bishop, Dec 12, 2003
    #2
    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. Koen
    Replies:
    4
    Views:
    383
    Sudsy
    Oct 27, 2004
  2. Klixx0r

    Problems adding floats

    Klixx0r, Dec 12, 2004, in forum: Java
    Replies:
    5
    Views:
    6,682
    Mark Thornton
    Dec 13, 2004
  3. Replies:
    7
    Views:
    5,372
    Googmeister
    Sep 8, 2005
  4. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,285
    Tim Rentsch
    Sep 23, 2005
  5. clumsy_ninja

    Problem formatting forms w/ floats

    clumsy_ninja, Feb 16, 2008, in forum: HTML
    Replies:
    2
    Views:
    395
    Jukka K. Korpela
    Feb 17, 2008
Loading...

Share This Page