473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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.
Jul 18 '05 #1
2 2101
<be*******@aol.com> schrieb im Newsbeitrag
news:30**************************@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

Jul 18 '05 #2

"vincent wehren" <vi*****@visualtrans.de> schrieb im Newsbeitrag
news:c2**********@news2.tilbu1.nb.home.nl...
| <be*******@aol.com> schrieb im Newsbeitrag
| news:30**************************@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
|
|
|
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Batista, Facundo | last post by:
One detail I found, don't now if it's ok: Python 2.1.1 (#4, Mar 8 2002, 12:32:24) on sunos5 >>> a = 'ñ' >>> a '\xf1' >>> print a ñ
11
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
2
by: Bernard Delmée | last post by:
Is there a simple way to modify the default sprintf mask used for floats ? (eg something like sys.float_mask = '%.2f') I've tried assigning to float.__dict__, but that's apparently not allowed.
1
by: Stan Brown | last post by:
On every page I have a <p ID="URhere"> that appears only when you print. (It's "display:none" in the general style sheet.) It would be nice if I could suggest that the browser print it at the...
11
by: hg | last post by:
Hi, Considering the float 0.0, I would like to print 00.00. I tried '%02.02f' % 0.0 ... but I get 0.00 Any clue ? Thanks,
17
by: Martin Wells | last post by:
When passing arguments to a VAL function, all integer types are promoted, and floats become doubles. Therefore, signed short will be promoted to signed int. Therefore I can't see any reason why...
11
by: A.T.Hofkamp | last post by:
Hello all, Yesterday we found the cause of a bug that has caused problems for a long time. It appeared to be the following: class A(object): pass print min(1.0, A())
2
by: ravir81 | last post by:
Hi, I am working on excel reporting using Perl. I am facing problem with writing the header part only once for all the excels created using Perl. Here is the code : ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.