Finding field widths of floats for neat printing

Andrew Gregory andrew.gregory at npl.co.uk
Thu Apr 3 09:45:25 EST 2003


I want to write lists of floating point numbers to text files in neat
columns with decimal points aligned, right-hand edges straight (rather
than ragged), and without unnecessary trailing zeros, e.g.
1.1    -34.67     0.006  
2.0     -1.05     0.010  
etc.

In order to do this I need to loop through the lists of numbers for
each column to find the field sizes m and n need to achieve this. I
can then produce a format specifier string (fss) for each column,
e.g. fss = "%" + "%i.%if" % (m,n)

Given a number, -34.67 for example is there a SMART Python way of
determining the field sizes? Obviously I need to work at less than the
precision of the machine, and ignore the fact that -34.67 might be
represented as
-34.6699999999999

I know that there are messy arithmetic methods (can use logs) - and
have implemented one.

Andrew.




More information about the Python-list mailing list