Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > default print format for floats

Reply
Thread Tools

default print format for floats

 
 
beliavsky@aol.com
Guest
Posts: n/a
 
      03-01-2004
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.
 
Reply With Quote
 
 
 
 
vincent wehren
Guest
Posts: n/a
 
      03-01-2004
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
vincent wehren
Guest
Posts: n/a
 
      03-01-2004

"vincent wehren" <(E-Mail Removed)> schrieb im Newsbeitrag
news:c201sl$aif$(E-Mail Removed)1.nb.home.nl...
| <(E-Mail Removed)> schrieb im Newsbeitrag
| news:(E-Mail Removed) 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
|
|
|


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making decimal default datatype for floats during runtime Stefan Sonnenberg-Carstens Python 0 06-21-2007 07:34 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
Converting from Microsoft Binary Format floats to Python Float geskerrett@hotmail.com Python 2 08-28-2005 01:22 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments