String formatting with fixed width

  • Thread starter Alexander Eisenhuth
  • Start date
A

Alexander Eisenhuth

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'21.100'

But there are missing ' '. How can I get that? (For bigger numbers than 999 they
might be cut: 1021 -> 021)

Thanks,
Alexander
 
S

Steve Holden

Alexander said:
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
'21.100'

But there are missing ' '. How can I get that? (For bigger numbers than 999 they
might be cut: 1021 -> 021)
... return "%7.3f" % (x % 1000.0)
... ... 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
 
J

Jon Clements

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
'21.100'

But there are missing ' '. How can I get that? (For bigger numbers than 999 they
might be cut: 1021 -> 021)

You can use something like this:' 21.100'


However, this will only make the string at *least* 7 long. If the
length of the number exceeds this, you'll end up with it even longer;
for instance:
'2123123121.100'

From your example about 'cutting' it, it looks like you just might be
able to take the last 7 chars from it using [-7:]

hth

Jon.
 
S

Steve Holden

A

Alexander Eisenhuth

Thanks for your fast reply. I'm fine with your "%7.03f" solution. (negatives are
not significant)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top