String formatting with fixed width
Steve Holden
steve at holdenweb.com
Fri Mar 16 09:28:49 EDT 2007
Alexander Eisenhuth wrote:
> Hello alltogether,
>
> is it possible to format stings with fixed width of let's say 7 character. T
> need a floating point with 3 chars before dot, padded with ' ' and 3 chars after
> dot, padded with '0'.
>
> Followingh is my approach
> >>> f = 21.1
> >>> s = "%.03f" % f
> >>> s
> '21.100'
>
> But there are missing ' '. How can I get that? (For bigger numbers than 999 they
> might be cut: 1021 -> 021)
>
>>> def f(x):
... return "%7.3f" % (x % 1000.0)
...
>>> for x in (9.9, 99.9, 999.9, 9999.9):
... print f(x)
...
9.900
99.900
999.900
999.900
>>>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
More information about the Python-list
mailing list