disruptive
disruptive

Reputation: 5946

Old style python formatting issues

I looking to display numeric data right justified with a forced sign and spaces for the mantissa such that the decimals all align for each column. The new format specifier uses ">" to align, but I'm unable to get something working with the "c style" format.

For example I'm using:

'%+7.2f \n' % (data)

How do I get the alignment? Is this possible with this old style formatting? I'm looking to get the decimal places all aligned up...seems a silly question, but can't seem to get anything other using the .format command.

Upvotes: 0

Views: 296

Answers (1)

John Gaines Jr.
John Gaines Jr.

Reputation: 11534

That will work, you just have to remember that the first number (the 7 in your example) is the total width of the column including all digits before and after the decimal place and the decimal place and the leading +.

>>> for n in (0.12345, 12345.6, 123):
...     print '%+9.2f' % (n)
    +0.12
+12345.60
  +123.00
>>> 

Upvotes: 1

Related Questions