[Tutor] Making table

Carroll, Barry Barry.Carroll at psc.com
Mon Mar 19 23:52:23 CET 2007


> -----Original Message-----
> From: Kent Johnson [mailto:kent37 at tds.net]
> Sent: Monday, March 19, 2007 12:59 PM
> To: Carroll, Barry
> Cc: tutor at python.org
> Subject: Re: [Tutor] Making table
> 
> Carroll, Barry wrote:
> >> -----Original Message-----
> >> Date: Mon, 19 Mar 2007 11:53:06 -0400
> >> From: Kent Johnson <kent37 at tds.net>
> 
> >> Most string formatting conversions allow you to specify a width
> >> directly. For example,
> >> In [61]: value = 3.45678
> >> In [63]: "%10.3f" % value
> >> Out[63]: '     3.457'
> >>
> >> Kent
> >>
> > What if one wished to align the values in each column at the decimal
> > point?  Is there a simple means to do this in Python, or would one
need
> > to write a new function?
> 
> If you specify the number of digits after the decimal point, that
number
> of digits will always be given, so columns will line up:
> In [86]: '%10.3f' % 1.2
> Out[86]: '     1.200'
> In [87]: '%10.3f' % 1.23456
> Out[87]: '     1.235'
> 
> If you want something fancier I think you will have to do it yourself.
> 
> Kent
Kent:

Thanks for your response.  That method is certainly good enough for
general use.  What about a case where the user needs to keep track of
significant digits? As an example, assume that the following values
(pulled arbitrarily from my head, I admit) are accurate to three
significant digits:

	253.
	77.6
	9.03
	.0210

Using a formatting string of "%10.4f", these would be rendered as:

	'  253.0000'
	'   77.6000
	'    9.0300'
	'    0.0210'

This formatting gives the impression that all values but the last are
more precise than they truly are.  A scientist or statistician would
prefer to see something like this:

	'254.    '
	' 77.6   '
	'  9.03  '
	'  0.0210'

Does numpy or some other math package have that capability?

Thanks again.
 
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed





More information about the Tutor mailing list