default print format for floats

B

beliavsky

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.
 
V

vincent wehren

| 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
 
V

vincent wehren

| | | 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
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top