Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   default print format for floats (http://www.velocityreviews.com/forums/t329019-default-print-format-for-floats.html)

 beliavsky@aol.com 03-01-2004 06:46 PM

default print format for floats

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.

 vincent wehren 03-01-2004 07:11 PM

Re: default print format for floats

<beliavsky@aol.com> schrieb im Newsbeitrag
news:3064b51d.0403011046.4238998d@posting.google.c om...
| 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 03-01-2004 07:45 PM

Re: default print format for floats

"vincent wehren" <vincent@visualtrans.de> schrieb im Newsbeitrag
news:c201sl\$aif\$1@news2.tilbu1.nb.home.nl...
| <beliavsky@aol.com> schrieb im Newsbeitrag
| news:3064b51d.0403011046.4238998d@posting.google.c om...
| | 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
|
|
|

 All times are GMT. The time now is 02:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.