Align numbers at decimal point
Peter Otten
__peter__ at web.de
Wed Sep 15 05:38:01 EDT 2004
Enno Middelberg wrote:
> I want to print out columns of numbers which are aligned at the decimal
> point, eg:
>
> 123.45
> 3.0
> 65.765486
One way to do it:
>>> def adjust(f, width, dec):
... pos = f.find(".")
... if pos == -1:
... pos = len(f)
... dec += 1
... return "%*s%-*s" % (width-dec, f[:pos], dec, f[pos:])
...
>>> print "\n".join([repr(adjust("%s" % f, 10, 5)) for f in [1, 2.3456,
42.42]])
' 1 '
' 2.3456 '
' 42.42 '
Peter
More information about the Python-list
mailing list