default print format for floats

Discussion in 'Python' started by beliavsky@aol.com, Mar 1, 2004.

  1. Guest

    By default, Python prints many floating point numbers with 17 digits
    after the decimal place. I would like to make the DEFAULT 4 decimal
    places. Is this possible?

    For example, the code

    from string import join
    x = [1.0,0.3,0.4]
    print x
    print join(["%4.1f" % y for y in x])

    gives the output

    [1.0, 0.29999999999999999, 0.40000000000000002]
    1.0 0.3 0.4

    I want the simplicity of "print x", without so many decimal places.
    , Mar 1, 2004
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    | By default, Python prints many floating point numbers with 17 digits
    | after the decimal place. I would like to make the DEFAULT 4 decimal
    | places. Is this possible?
    |
    | For example, the code
    |
    | from string import join
    | x = [1.0,0.3,0.4]
    | print x
    | print join(["%4.1f" % y for y in x])
    |
    | gives the output
    |
    | [1.0, 0.29999999999999999, 0.40000000000000002]
    | 1.0 0.3 0.4
    |
    | I want the simplicity of "print x", without so many decimal places.

    I wouldn't cjanging "DEFAULT BEHAVIOR", but a slight modification of the
    above
    gives you:

    >>> x = [1.0, 0.3, 0.4]
    >>> print ", ".join(["%.4f" % y for y in x])

    1.0000, 0.3000, 0.4000

    Is this what you're after?

    HTH,

    Vincent Wehren
    vincent wehren, Mar 1, 2004
    #2
    1. Advertising

  3. "vincent wehren" <> schrieb im Newsbeitrag
    news:c201sl$aif$1.nb.home.nl...
    | <> schrieb im Newsbeitrag
    | news:...
    | | By default, Python prints many floating point numbers with 17 digits
    | | after the decimal place. I would like to make the DEFAULT 4 decimal
    | | places. Is this possible?
    | |
    | | For example, the code
    | |
    | | from string import join
    | | x = [1.0,0.3,0.4]
    | | print x
    | | print join(["%4.1f" % y for y in x])
    | |
    | | gives the output
    | |
    | | [1.0, 0.29999999999999999, 0.40000000000000002]
    | | 1.0 0.3 0.4
    | |
    | | I want the simplicity of "print x", without so many decimal places.
    |


    | I wouldn't cjanging "DEFAULT BEHAVIOR", but a slight modification of the

    This was supposed to read "I wouldn't call it changing "DEFAULT BEHAVIOR"...
    Don't know what my fingers where thinking ;)

    Vincent


    | above
    | gives you:
    |
    | >>> x = [1.0, 0.3, 0.4]
    | >>> print ", ".join(["%.4f" % y for y in x])
    | 1.0000, 0.3000, 0.4000
    |
    | Is this what you're after?
    |
    | HTH,
    |
    | Vincent Wehren
    |
    |
    |
    vincent wehren, Mar 1, 2004
    #3
    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. Replies:
    2
    Views:
    522
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,281
    Tim Rentsch
    Sep 23, 2005
  3. keto
    Replies:
    0
    Views:
    924
  4. Stefan Sonnenberg-Carstens

    Making decimal default datatype for floats during runtime

    Stefan Sonnenberg-Carstens, Jun 21, 2007, in forum: Python
    Replies:
    0
    Views:
    251
    Stefan Sonnenberg-Carstens
    Jun 21, 2007
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    345
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page