Quick: Format Specs in %

Cliff Wells logiplexsoftware at earthlink.net
Wed Apr 24 16:59:41 EDT 2002


On 24 Apr 2002 20:41:14 GMT
Philip Swartzleonard wrote:

> 
> '%2f' % some_number pads the number with 2 blank spaces
> '%.2f' % some_number fixes the number to 2 decimal places
> 
> the problem:
> '%2.2f' % some_number only does the second of those two things. I need 
> both. I tried looking up the % operator in the index of the language 
> reference, but couldn't find it.

The first digit in %2.2f specifies how wide the field is, not how many spaces
to pad with.

>>> f
3.1415926535897931
>>> print "%2f" % f
3.141593
>>> print "%.2f" % f
3.14
>>> print "%10.2f" % f
      3.14
^^^^^^  <- 6 spaces + 4 characters in the number = 10 

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308





More information about the Python-list mailing list