default print format for floats

vincent wehren vincent at visualtrans.de
Mon Mar 1 14:11:12 EST 2004


<beliavsky at aol.com> schrieb im Newsbeitrag
news:3064b51d.0403011046.4238998d at posting.google.com...
| 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






More information about the Python-list mailing list